Sample code for 30+ languages & platforms
PureBasic Requires Chilkat v11.7.0+

Box: Create a Project Folder, Upload, Tag, and Link a Document

See more Box Examples

Run the reusable setup once per HttpCurl object, then select names and execute the operation. The setup is repeated here only to make this published example independently runnable. Resolve the parent documents folder by name, create a new project folder, upload a document, apply pre-existing projectDocument metadata, and create a collaborators-only link. Creation outputs remain cached for subsequent operations; the engine resolves the enterprise scope only when metadata needs it. Multipart formatting and deliberate writes remain explicit.

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

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkHttpCurl.pb"
IncludeFile "CkStringBuilder.pb"

Procedure ChilkatExample()

    success.i = 0

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

    success = 0

    ;  BEGIN ONE-TIME SETUP
    ;  This block is repeated so each published example can run on its own.
    ;  In an application, run the setup example once and pass the SAME curl object
    ;  to operation code below. Do not recreate it for every operation.
    ;  See Reusable Box HttpCurl Setup.
    curl.i = CkHttpCurl::ckCreate()
    If curl.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkHttpCurl::ckSetVar(curl,"access_token","BOX_ACCESS_TOKEN")
    CkHttpCurl::ckSetVar(curl,"parent_name","/")
    ;  Box's fixed root ID is an implementation detail; callers supply names.
    CkHttpCurl::ckSetVar(curl,"parent_id","0")
    setupCommand.i = CkStringBuilder::ckCreate()
    If setupCommand.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    setupReplacements.i

    ;  Register findParent; registration sends no request.
    CkStringBuilder::ckSetString(setupCommand,"curl -X GET " + Chr(34) + "https://api.box.com/2.0/folders/0/items?limit=1000&fields=id,type,name" + Chr(34) + " -H " + Chr(34) + "Authorization: Bearer ACCESS_TOKEN" + Chr(34))
    setupReplacements = CkStringBuilder::ckReplace(setupCommand,"ACCESS_TOKEN",CkHttpCurl::ckGetVar(curl,"access_token"))
    success = CkHttpCurl::ckAddFunction(curl,"findParent",CkStringBuilder::ckGetAsString(setupCommand))
    If success <> 1
        Debug CkHttpCurl::ckLastErrorText(curl)
        CkHttpCurl::ckDispose(curl)
        CkStringBuilder::ckDispose(setupCommand)
        ProcedureReturn
    EndIf

    ;  Register findFolder; registration sends no request.
    CkStringBuilder::ckSetString(setupCommand,"curl -X GET " + Chr(34) + "https://api.box.com/2.0/folders/{{parent_id}}/items?limit=1000&fields=id,type,name" + Chr(34) + " -H " + Chr(34) + "Authorization: Bearer ACCESS_TOKEN" + Chr(34))
    setupReplacements = CkStringBuilder::ckReplace(setupCommand,"ACCESS_TOKEN",CkHttpCurl::ckGetVar(curl,"access_token"))
    success = CkHttpCurl::ckAddFunction(curl,"findFolder",CkStringBuilder::ckGetAsString(setupCommand))
    If success <> 1
        Debug CkHttpCurl::ckLastErrorText(curl)
        CkHttpCurl::ckDispose(curl)
        CkStringBuilder::ckDispose(setupCommand)
        ProcedureReturn
    EndIf

    ;  Register findFile; registration sends no request.
    CkStringBuilder::ckSetString(setupCommand,"curl -X GET " + Chr(34) + "https://api.box.com/2.0/folders/{{folder_id}}/items?limit=1000&fields=id,type,name" + Chr(34) + " -H " + Chr(34) + "Authorization: Bearer ACCESS_TOKEN" + Chr(34))
    setupReplacements = CkStringBuilder::ckReplace(setupCommand,"ACCESS_TOKEN",CkHttpCurl::ckGetVar(curl,"access_token"))
    success = CkHttpCurl::ckAddFunction(curl,"findFile",CkStringBuilder::ckGetAsString(setupCommand))
    If success <> 1
        Debug CkHttpCurl::ckLastErrorText(curl)
        CkHttpCurl::ckDispose(curl)
        CkStringBuilder::ckDispose(setupCommand)
        ProcedureReturn
    EndIf

    ;  Register findDestination; registration sends no request.
    CkStringBuilder::ckSetString(setupCommand,"curl -X GET " + Chr(34) + "https://api.box.com/2.0/folders/0/items?limit=1000&fields=id,type,name" + Chr(34) + " -H " + Chr(34) + "Authorization: Bearer ACCESS_TOKEN" + Chr(34))
    setupReplacements = CkStringBuilder::ckReplace(setupCommand,"ACCESS_TOKEN",CkHttpCurl::ckGetVar(curl,"access_token"))
    success = CkHttpCurl::ckAddFunction(curl,"findDestination",CkStringBuilder::ckGetAsString(setupCommand))
    If success <> 1
        Debug CkHttpCurl::ckLastErrorText(curl)
        CkHttpCurl::ckDispose(curl)
        CkStringBuilder::ckDispose(setupCommand)
        ProcedureReturn
    EndIf

    ;  Register getFileDetails; registration sends no request.
    CkStringBuilder::ckSetString(setupCommand,"curl -X GET " + Chr(34) + "https://api.box.com/2.0/files/{{file_id}}?fields=id,sha1,file_version" + Chr(34) + " -H " + Chr(34) + "Authorization: Bearer ACCESS_TOKEN" + Chr(34))
    setupReplacements = CkStringBuilder::ckReplace(setupCommand,"ACCESS_TOKEN",CkHttpCurl::ckGetVar(curl,"access_token"))
    success = CkHttpCurl::ckAddFunction(curl,"getFileDetails",CkStringBuilder::ckGetAsString(setupCommand))
    If success <> 1
        Debug CkHttpCurl::ckLastErrorText(curl)
        CkHttpCurl::ckDispose(curl)
        CkStringBuilder::ckDispose(setupCommand)
        ProcedureReturn
    EndIf

    ;  Register getEnterprise; registration sends no request.
    CkStringBuilder::ckSetString(setupCommand,"curl -X GET " + Chr(34) + "https://api.box.com/2.0/users/me?fields=enterprise" + Chr(34) + " -H " + Chr(34) + "Authorization: Bearer ACCESS_TOKEN" + Chr(34))
    setupReplacements = CkStringBuilder::ckReplace(setupCommand,"ACCESS_TOKEN",CkHttpCurl::ckGetVar(curl,"access_token"))
    success = CkHttpCurl::ckAddFunction(curl,"getEnterprise",CkStringBuilder::ckGetAsString(setupCommand))
    If success <> 1
        Debug CkHttpCurl::ckLastErrorText(curl)
        CkHttpCurl::ckDispose(curl)
        CkStringBuilder::ckDispose(setupCommand)
        ProcedureReturn
    EndIf

    ;  Register output rules once. Chilkat 11.7.0 evaluates {{name}} variables
    ;  when extracting each output, using the current value at that time.
    success = CkHttpCurl::ckAddOutput2(curl,"findParent","entries","name","{{parent_name}}",1,"id","parent_id")
    If success <> 1
        Debug CkHttpCurl::ckLastErrorText(curl)
        CkHttpCurl::ckDispose(curl)
        CkStringBuilder::ckDispose(setupCommand)
        ProcedureReturn
    EndIf

    success = CkHttpCurl::ckAddOutput2(curl,"findFolder","entries","name","{{folder_name}}",1,"id","folder_id")
    If success <> 1
        Debug CkHttpCurl::ckLastErrorText(curl)
        CkHttpCurl::ckDispose(curl)
        CkStringBuilder::ckDispose(setupCommand)
        ProcedureReturn
    EndIf

    success = CkHttpCurl::ckAddOutput2(curl,"findFile","entries","name","{{file_name}}",1,"id","file_id")
    If success <> 1
        Debug CkHttpCurl::ckLastErrorText(curl)
        CkHttpCurl::ckDispose(curl)
        CkStringBuilder::ckDispose(setupCommand)
        ProcedureReturn
    EndIf

    success = CkHttpCurl::ckAddOutput2(curl,"findDestination","entries","name","{{destination_name}}",1,"id","destination_id")
    If success <> 1
        Debug CkHttpCurl::ckLastErrorText(curl)
        CkHttpCurl::ckDispose(curl)
        CkStringBuilder::ckDispose(setupCommand)
        ProcedureReturn
    EndIf

    success = CkHttpCurl::ckAddOutput(curl,"getFileDetails","file_version.id","file_version_id")
    If success <> 1
        Debug CkHttpCurl::ckLastErrorText(curl)
        CkHttpCurl::ckDispose(curl)
        CkStringBuilder::ckDispose(setupCommand)
        ProcedureReturn
    EndIf

    success = CkHttpCurl::ckAddOutput(curl,"getFileDetails","sha1","file_sha1")
    If success <> 1
        Debug CkHttpCurl::ckLastErrorText(curl)
        CkHttpCurl::ckDispose(curl)
        CkStringBuilder::ckDispose(setupCommand)
        ProcedureReturn
    EndIf

    success = CkHttpCurl::ckAddOutput(curl,"getEnterprise","enterprise.id","enterprise_id")
    If success <> 1
        Debug CkHttpCurl::ckLastErrorText(curl)
        CkHttpCurl::ckDispose(curl)
        CkStringBuilder::ckDispose(setupCommand)
        ProcedureReturn
    EndIf

    ;  END ONE-TIME SETUP
    ;  The name lookups below assume each listing fits in one page (1000 items).
    ;  Parent and destination names select root children; folder_name selects a child
    ;  of parent_name. "/" selects the root parent. These are not arbitrary paths.

    ;  BEGIN OPERATION -- keep the configured object when reusing it.
    ;  Inputs are names. Retain cached IDs when the selected names have not changed.
    parentName.s = "documents"
    If (CkHttpCurl::ckGetVar(curl,"parent_name") <> parentName) OR ((parentName = "/") AND (CkHttpCurl::ckVarDefined(curl,"parent_id") = 0))
        CkHttpCurl::ckSetVar(curl,"parent_name",parentName)
        CkHttpCurl::ckClearVar(curl,"parent_id")
        CkHttpCurl::ckClearVar(curl,"folder_id")
        CkHttpCurl::ckClearVar(curl,"file_id")
        CkHttpCurl::ckClearVar(curl,"file_version_id")
        CkHttpCurl::ckClearVar(curl,"file_sha1")
        If parentName = "/"
            CkHttpCurl::ckSetVar(curl,"parent_id","0")
        EndIf

    EndIf

    CkHttpCurl::ckSetVar(curl,"new_folder_name","Project CUST-1001")
    ;  This workflow creates a new folder. A 409 stops the example; it does not
    ;  silently reuse a same-named folder. Retain printed IDs if a later step fails.
    CkHttpCurl::ckClearTargetOutput(curl,"*")
    CkHttpCurl::ckClearVar(curl,"folder_id")
    CkHttpCurl::ckAddTargetOutput(curl,"id","folder_id")
    authorizedCommand1.i = CkStringBuilder::ckCreate()
    If authorizedCommand1.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckSetString(authorizedCommand1,"curl -X POST " + Chr(34) + "https://api.box.com/2.0/folders" + Chr(34) + " -H " + Chr(34) + "Authorization: Bearer {{access_token}}" + Chr(34) + " -H " + Chr(34) + "Content-Type: application/json" + Chr(34) + " -d '{" + Chr(34) + "name" + Chr(34) + ":" + Chr(34) + "{{new_folder_name}}" + Chr(34) + "," + Chr(34) + "parent" + Chr(34) + ":{" + Chr(34) + "id" + Chr(34) + ":" + Chr(34) + "{{parent_id}}" + Chr(34) + "}}'")
    headerReplacement1.i = CkStringBuilder::ckReplace(authorizedCommand1,"{{access_token}}",CkHttpCurl::ckGetVar(curl,"access_token"))
    success = CkHttpCurl::ckDoYourThing(curl,CkStringBuilder::ckGetAsString(authorizedCommand1))
    If (success <> 1) OR (CkHttpCurl::ckStatusCode(curl) <> 201)
        Debug "Box request failed. HTTP status: " + Str(CkHttpCurl::ckStatusCode(curl))
        Debug "Failure reason: " + Str(CkHttpCurl::ckFailReason(curl))
        Debug CkHttpCurl::ckResponseBodyStr(curl)
        CkHttpCurl::ckDispose(curl)
        CkStringBuilder::ckDispose(setupCommand)
        CkStringBuilder::ckDispose(authorizedCommand1)
        ProcedureReturn
    EndIf

    If CkHttpCurl::ckVarDefined(curl,"!") <> 1
        Debug "Expected response fields are missing."
        CkHttpCurl::ckDispose(curl)
        CkStringBuilder::ckDispose(setupCommand)
        CkStringBuilder::ckDispose(authorizedCommand1)
        ProcedureReturn
    EndIf

    Debug "Created folder: " + CkHttpCurl::ckGetVar(curl,"folder_id")
    ;  Resolve the multipart attributes field before passing the command to HttpCurl.
    ;  Box folder IDs come from configuration or a previous API response.
    uploadCommand.i = CkStringBuilder::ckCreate()
    If uploadCommand.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    authorizedCommand2.i = CkStringBuilder::ckCreate()
    If authorizedCommand2.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckSetString(authorizedCommand2,"curl -X POST " + Chr(34) + "https://upload.box.com/api/2.0/files/content" + Chr(34) + " -H " + Chr(34) + "Authorization: Bearer {{access_token}}" + Chr(34) + " -F 'attributes={" + Chr(34) + "name" + Chr(34) + ":" + Chr(34) + "invoice.pdf" + Chr(34) + "," + Chr(34) + "parent" + Chr(34) + ":{" + Chr(34) + "id" + Chr(34) + ":" + Chr(34) + "{{folder_id}}" + Chr(34) + "}}' -F " + Chr(34) + "file=@qa_data/pdf/invoice.pdf;type=application/pdf" + Chr(34))
    headerReplacement2.i = CkStringBuilder::ckReplace(authorizedCommand2,"{{access_token}}",CkHttpCurl::ckGetVar(curl,"access_token"))
    CkStringBuilder::ckSetString(uploadCommand,CkStringBuilder::ckGetAsString(authorizedCommand2))
    replacements.i = CkStringBuilder::ckReplace(uploadCommand,"{{folder_id}}",CkHttpCurl::ckGetVar(curl,"folder_id"))
    CkHttpCurl::ckClearTargetOutput(curl,"*")
    CkHttpCurl::ckClearVar(curl,"file_id")
    CkHttpCurl::ckAddTargetOutput(curl,"entries[0].id","file_id")
    success = CkHttpCurl::ckDoYourThing(curl,CkStringBuilder::ckGetAsString(uploadCommand))
    If (success <> 1) OR (CkHttpCurl::ckStatusCode(curl) <> 201)
        Debug "Box request failed. HTTP status: " + Str(CkHttpCurl::ckStatusCode(curl))
        Debug "Failure reason: " + Str(CkHttpCurl::ckFailReason(curl))
        Debug CkHttpCurl::ckResponseBodyStr(curl)
        CkHttpCurl::ckDispose(curl)
        CkStringBuilder::ckDispose(setupCommand)
        CkStringBuilder::ckDispose(authorizedCommand1)
        CkStringBuilder::ckDispose(uploadCommand)
        CkStringBuilder::ckDispose(authorizedCommand2)
        ProcedureReturn
    EndIf

    If CkHttpCurl::ckVarDefined(curl,"!") <> 1
        Debug "Expected response fields are missing."
        CkHttpCurl::ckDispose(curl)
        CkStringBuilder::ckDispose(setupCommand)
        CkStringBuilder::ckDispose(authorizedCommand1)
        CkStringBuilder::ckDispose(uploadCommand)
        CkStringBuilder::ckDispose(authorizedCommand2)
        ProcedureReturn
    EndIf

    Debug "Uploaded file: " + CkHttpCurl::ckGetVar(curl,"file_id")
    CkHttpCurl::ckClearTargetOutput(curl,"*")
    authorizedCommand3.i = CkStringBuilder::ckCreate()
    If authorizedCommand3.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckSetString(authorizedCommand3,"curl -X POST " + Chr(34) + "https://api.box.com/2.0/files/{{file_id}}/metadata/enterprise_{{enterprise_id}}/projectDocument" + Chr(34) + " -H " + Chr(34) + "Authorization: Bearer {{access_token}}" + Chr(34) + " -H " + Chr(34) + "Content-Type: application/json" + Chr(34) + " -d '{" + Chr(34) + "customerId" + Chr(34) + ":" + Chr(34) + "CUST-1001" + Chr(34) + "," + Chr(34) + "status" + Chr(34) + ":" + Chr(34) + "received" + Chr(34) + "}'")
    headerReplacement3.i = CkStringBuilder::ckReplace(authorizedCommand3,"{{access_token}}",CkHttpCurl::ckGetVar(curl,"access_token"))
    success = CkHttpCurl::ckDoYourThing(curl,CkStringBuilder::ckGetAsString(authorizedCommand3))
    If (success <> 1) OR (CkHttpCurl::ckStatusCode(curl) <> 201)
        Debug "Box request failed. HTTP status: " + Str(CkHttpCurl::ckStatusCode(curl))
        Debug "Failure reason: " + Str(CkHttpCurl::ckFailReason(curl))
        Debug CkHttpCurl::ckResponseBodyStr(curl)
        CkHttpCurl::ckDispose(curl)
        CkStringBuilder::ckDispose(setupCommand)
        CkStringBuilder::ckDispose(authorizedCommand1)
        CkStringBuilder::ckDispose(uploadCommand)
        CkStringBuilder::ckDispose(authorizedCommand2)
        CkStringBuilder::ckDispose(authorizedCommand3)
        ProcedureReturn
    EndIf

    CkHttpCurl::ckClearTargetOutput(curl,"*")
    CkHttpCurl::ckClearVar(curl,"shared_url")
    CkHttpCurl::ckAddTargetOutput(curl,"shared_link.url","shared_url")
    authorizedCommand4.i = CkStringBuilder::ckCreate()
    If authorizedCommand4.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckSetString(authorizedCommand4,"curl -X PUT " + Chr(34) + "https://api.box.com/2.0/files/{{file_id}}?fields=id,shared_link" + Chr(34) + " -H " + Chr(34) + "Authorization: Bearer {{access_token}}" + Chr(34) + " -H " + Chr(34) + "Content-Type: application/json" + Chr(34) + " -d '{" + Chr(34) + "shared_link" + Chr(34) + ":{" + Chr(34) + "access" + Chr(34) + ":" + Chr(34) + "collaborators" + Chr(34) + "}}'")
    headerReplacement4.i = CkStringBuilder::ckReplace(authorizedCommand4,"{{access_token}}",CkHttpCurl::ckGetVar(curl,"access_token"))
    success = CkHttpCurl::ckDoYourThing(curl,CkStringBuilder::ckGetAsString(authorizedCommand4))
    If (success <> 1) OR (CkHttpCurl::ckStatusCode(curl) <> 200)
        Debug "Box request failed. HTTP status: " + Str(CkHttpCurl::ckStatusCode(curl))
        Debug "Failure reason: " + Str(CkHttpCurl::ckFailReason(curl))
        Debug CkHttpCurl::ckResponseBodyStr(curl)
        CkHttpCurl::ckDispose(curl)
        CkStringBuilder::ckDispose(setupCommand)
        CkStringBuilder::ckDispose(authorizedCommand1)
        CkStringBuilder::ckDispose(uploadCommand)
        CkStringBuilder::ckDispose(authorizedCommand2)
        CkStringBuilder::ckDispose(authorizedCommand3)
        CkStringBuilder::ckDispose(authorizedCommand4)
        ProcedureReturn
    EndIf

    If CkHttpCurl::ckVarDefined(curl,"!") <> 1
        Debug "Expected response fields are missing."
        CkHttpCurl::ckDispose(curl)
        CkStringBuilder::ckDispose(setupCommand)
        CkStringBuilder::ckDispose(authorizedCommand1)
        CkStringBuilder::ckDispose(uploadCommand)
        CkStringBuilder::ckDispose(authorizedCommand2)
        CkStringBuilder::ckDispose(authorizedCommand3)
        CkStringBuilder::ckDispose(authorizedCommand4)
        ProcedureReturn
    EndIf

    Debug "Document link: " + CkHttpCurl::ckGetVar(curl,"shared_url")
    ;  The link is for existing collaborators. Invite users separately when needed.
    ;  ClearTargetOutput removes old extraction rules; it does not clear existing IDs.
    CkHttpCurl::ckSetVar(curl,"folder_name",CkHttpCurl::ckGetVar(curl,"new_folder_name"))
    CkHttpCurl::ckSetVar(curl,"file_name","invoice.pdf")
    CkHttpCurl::ckClearVar(curl,"file_version_id")
    CkHttpCurl::ckClearVar(curl,"file_sha1")


    CkHttpCurl::ckDispose(curl)
    CkStringBuilder::ckDispose(setupCommand)
    CkStringBuilder::ckDispose(authorizedCommand1)
    CkStringBuilder::ckDispose(uploadCommand)
    CkStringBuilder::ckDispose(authorizedCommand2)
    CkStringBuilder::ckDispose(authorizedCommand3)
    CkStringBuilder::ckDispose(authorizedCommand4)


    ProcedureReturn
EndProcedure