Sample code for 30+ languages & platforms
PowerShell 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 PowerShell Downloads

PowerShell
Add-Type -Path "C:\chilkat\ChilkatDotNet47-x64\ChilkatDotNet47.dll"

$success = $false

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

$success = $false

#  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.
$curl = New-Object Chilkat.HttpCurl
$curl.SetVar("access_token","BOX_ACCESS_TOKEN")

$state = New-Object Chilkat.JsonObject
$files = New-Object Chilkat.FileAccess
$statePath = "qa_data/box_events_state.json"
if ($files.FileExists($statePath) -eq $true) {
    $success = $state.LoadFile($statePath)
    if ($success -ne $true) {
        $($state.LastErrorText)
        exit
    }

    if ($state.HasMember("position") -ne $true) {
        $("Invalid checkpoint: position missing.")
        exit
    }

}
else {
    #  "now" starts a new subscription. It deliberately skips historical events.
    $state.UpdateString("position","now")
}

$curl.SetVar("position",$state.StringOf("position"))
$page = New-Object Chilkat.JsonObject
$seenPath = New-Object Chilkat.StringBuilder

$batch = 0

$more = $true
while (($more -eq $true) -and ($batch -lt 20)) {
    $curl.ClearTargetOutput("*")
    $authorizedCommand1 = New-Object Chilkat.StringBuilder
    $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 -ne $true) -or ($curl.StatusCode -ne 200)) {
        $("Box request failed. HTTP status: " + $curl.StatusCode)
        $("Failure reason: " + $curl.FailReason)
        $($curl.ResponseBodyStr)
        exit
    }

    $success = $curl.GetResponseJson($page)
    if ($success -ne $true) {
        $($curl.LastErrorText)
        exit
    }

    $count = $page.SizeOfArray("entries")
    $i = 0
    while ($i -lt $count) {
        $page.I = $i
        $eventId = $page.StringOf("entries[i].event_id")
        if ($eventId -eq "") {
            $("Event without an event_id; checkpoint not advanced.")
            exit
        }

        $seenPath.Clear()
        $seenPath.Append("seen.")
        $seenPath.Append($eventId)
        if ($state.HasMember($seenPath.GetAsString()) -eq $false) {
            #  Replace this logging with your idempotent business operation.
            #  Record success only after that operation succeeds.
            $($eventId + " " + $page.StringOf("entries[i].event_type") + " " + $page.StringOf("entries[i].source.id"))
            $state.UpdateBool($seenPath.GetAsString(),$true)
        }

        $i = $i + 1
    }

    $nextPosition = $page.StringOf("next_stream_position")
    if ($nextPosition -eq "") {
        $("Missing next_stream_position; checkpoint not advanced.")
        exit
    }

    $more = ($count -gt 0) -and ($nextPosition -ne $curl.GetVar("position"))
    $state.UpdateString("position",$nextPosition)
    $success = $state.WriteFile($statePath)
    if ($success -ne $true) {
        $($state.LastErrorText)
        exit
    }

    $curl.SetVar("position",$nextPosition)
    $batch = $batch + 1
}

$("Checkpoint saved. Run again later to continue polling.")