Sample code for 30+ languages & platforms
Classic ASP Requires Chilkat v11.5.0+

Box: Poll User Events and Save a Checkpoint

See more Box Examples

Poll the authenticated user event stream in bounded batches. The local JSON checkpoint keeps processed event IDs and the next stream position. This single-process demonstration logs events; a production worker should commit its business action, deduplication record, and cursor transactionally and prune old IDs.

See the Box API guide for setup and the complete workflow.

Chilkat Classic ASP Downloads

Classic ASP
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 0

'  Requires Chilkat 11.5.0 or later and a previously unlocked Chilkat bundle.
'  See Global Unlock Sample.
'  Guide: Box API Guide.

success = 0

'  Supply an unexpired token obtained with one of the Box authentication examples.
'  A token represents a particular user or service account. IDs must be accessible to it.
set curl = Server.CreateObject("Chilkat.HttpCurl")
curl.SetVar "access_token","BOX_ACCESS_TOKEN"

set state = Server.CreateObject("Chilkat.JsonObject")
set files = Server.CreateObject("Chilkat.FileAccess")
statePath = "qa_data/box_events_state.json"
If (files.FileExists(statePath) = 1) Then
    success = state.LoadFile(statePath)
    If (success <> 1) Then
        Response.Write "<pre>" & Server.HTMLEncode( state.LastErrorText) & "</pre>"
        Response.End
    End If

    If (state.HasMember("position") <> 1) Then
        Response.Write "<pre>" & Server.HTMLEncode( "Invalid checkpoint: position missing.") & "</pre>"
        Response.End
    End If

Else
    '  "now" starts a new subscription. It deliberately skips historical events.
    success = state.UpdateString("position","now")
End If

curl.SetVar "position",state.StringOf("position")
set page = Server.CreateObject("Chilkat.JsonObject")
set seenPath = Server.CreateObject("Chilkat.StringBuilder")

batch = 0

more = 1
Do While (more = 1) And (batch < 20)
    curl.ClearTargetOutput "*"
    set authorizedCommand1 = Server.CreateObject("Chilkat.StringBuilder")
    success = authorizedCommand1.SetString("curl -X GET ""https://api.box.com/2.0/events?stream_type=all&stream_position={{position}}&limit=100"" -H ""Authorization: Bearer {{access_token}}""")
    headerReplacement1 = authorizedCommand1.Replace("{{access_token}}",curl.GetVar("access_token"))
    success = curl.DoYourThing(authorizedCommand1.GetAsString())
    If ((success <> 1) Or (curl.StatusCode <> 200)) Then
        Response.Write "<pre>" & Server.HTMLEncode( "Box request failed. HTTP status: " & curl.StatusCode) & "</pre>"
        Response.Write "<pre>" & Server.HTMLEncode( "Failure reason: " & curl.FailReason) & "</pre>"
        Response.Write "<pre>" & Server.HTMLEncode( curl.ResponseBodyStr) & "</pre>"
        Response.End
    End If

    success = curl.GetResponseJson(page)
    If (success <> 1) Then
        Response.Write "<pre>" & Server.HTMLEncode( curl.LastErrorText) & "</pre>"
        Response.End
    End If

    count = page.SizeOfArray("entries")
    i = 0
    Do While i < count
        page.I = i
        eventId = page.StringOf("entries[i].event_id")
        If (eventId = "") Then
            Response.Write "<pre>" & Server.HTMLEncode( "Event without an event_id; checkpoint not advanced.") & "</pre>"
            Response.End
        End If

        seenPath.Clear 
        success = seenPath.Append("seen.")
        success = seenPath.Append(eventId)
        If (state.HasMember(seenPath.GetAsString()) = 0) Then
            '  Replace this logging with your idempotent business operation.
            '  Record success only after that operation succeeds.
            Response.Write "<pre>" & Server.HTMLEncode( eventId & " " & page.StringOf("entries[i].event_type") & " " & page.StringOf("entries[i].source.id")) & "</pre>"
            success = state.UpdateBool(seenPath.GetAsString(),1)
        End If

        i = i + 1
    Loop
    nextPosition = page.StringOf("next_stream_position")
    If (nextPosition = "") Then
        Response.Write "<pre>" & Server.HTMLEncode( "Missing next_stream_position; checkpoint not advanced.") & "</pre>"
        Response.End
    End If

    more = (count > 0) And (nextPosition <> curl.GetVar("position"))
    success = state.UpdateString("position",nextPosition)
    success = state.WriteFile(statePath)
    If (success <> 1) Then
        Response.Write "<pre>" & Server.HTMLEncode( state.LastErrorText) & "</pre>"
        Response.End
    End If

    curl.SetVar "position",nextPosition
    batch = batch + 1
Loop
Response.Write "<pre>" & Server.HTMLEncode( "Checkpoint saved. Run again later to continue polling.") & "</pre>"

%>
</body>
</html>