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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoCurl
    Handle hoSetupCommand
    Integer iSetupReplacements
    String sParentName
    Handle hoAuthorizedCommand1
    Integer iHeaderReplacement1
    Handle hoUploadCommand
    Handle hoAuthorizedCommand2
    Integer iHeaderReplacement2
    Integer iReplacements
    Handle hoAuthorizedCommand3
    Integer iHeaderReplacement3
    Handle hoAuthorizedCommand4
    Integer iHeaderReplacement4
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

    Move False To iSuccess

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

    Move False To iSuccess

    //  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.
    Get Create (RefClass(cComChilkatHttpCurl)) To hoCurl
    If (Not(IsComObjectCreated(hoCurl))) Begin
        Send CreateComObject of hoCurl
    End
    Send ComSetVar To hoCurl "access_token" "BOX_ACCESS_TOKEN"
    Send ComSetVar To hoCurl "parent_name" "/"
    //  Box's fixed root ID is an implementation detail; callers supply names.
    Send ComSetVar To hoCurl "parent_id" "0"
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSetupCommand
    If (Not(IsComObjectCreated(hoSetupCommand))) Begin
        Send CreateComObject of hoSetupCommand
    End

    //  Register findParent; registration sends no request.
    Get ComSetString Of hoSetupCommand 'curl -X GET "https://api.box.com/2.0/folders/0/items?limit=1000&fields=id,type,name" -H "Authorization: Bearer ACCESS_TOKEN"' To iSuccess
    Get ComGetVar Of hoCurl "access_token" To sTemp1
    Get ComReplace Of hoSetupCommand "ACCESS_TOKEN" sTemp1 To iSetupReplacements
    Get ComGetAsString Of hoSetupCommand To sTemp1
    Get ComAddFunction Of hoCurl "findParent" sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoCurl To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Register findFolder; registration sends no request.
    Get ComSetString Of hoSetupCommand 'curl -X GET "https://api.box.com/2.0/folders/{{parent_id}}/items?limit=1000&fields=id,type,name" -H "Authorization: Bearer ACCESS_TOKEN"' To iSuccess
    Get ComGetVar Of hoCurl "access_token" To sTemp1
    Get ComReplace Of hoSetupCommand "ACCESS_TOKEN" sTemp1 To iSetupReplacements
    Get ComGetAsString Of hoSetupCommand To sTemp1
    Get ComAddFunction Of hoCurl "findFolder" sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoCurl To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Register findFile; registration sends no request.
    Get ComSetString Of hoSetupCommand 'curl -X GET "https://api.box.com/2.0/folders/{{folder_id}}/items?limit=1000&fields=id,type,name" -H "Authorization: Bearer ACCESS_TOKEN"' To iSuccess
    Get ComGetVar Of hoCurl "access_token" To sTemp1
    Get ComReplace Of hoSetupCommand "ACCESS_TOKEN" sTemp1 To iSetupReplacements
    Get ComGetAsString Of hoSetupCommand To sTemp1
    Get ComAddFunction Of hoCurl "findFile" sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoCurl To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Register findDestination; registration sends no request.
    Get ComSetString Of hoSetupCommand 'curl -X GET "https://api.box.com/2.0/folders/0/items?limit=1000&fields=id,type,name" -H "Authorization: Bearer ACCESS_TOKEN"' To iSuccess
    Get ComGetVar Of hoCurl "access_token" To sTemp1
    Get ComReplace Of hoSetupCommand "ACCESS_TOKEN" sTemp1 To iSetupReplacements
    Get ComGetAsString Of hoSetupCommand To sTemp1
    Get ComAddFunction Of hoCurl "findDestination" sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoCurl To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Register getFileDetails; registration sends no request.
    Get ComSetString Of hoSetupCommand 'curl -X GET "https://api.box.com/2.0/files/{{file_id}}?fields=id,sha1,file_version" -H "Authorization: Bearer ACCESS_TOKEN"' To iSuccess
    Get ComGetVar Of hoCurl "access_token" To sTemp1
    Get ComReplace Of hoSetupCommand "ACCESS_TOKEN" sTemp1 To iSetupReplacements
    Get ComGetAsString Of hoSetupCommand To sTemp1
    Get ComAddFunction Of hoCurl "getFileDetails" sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoCurl To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Register getEnterprise; registration sends no request.
    Get ComSetString Of hoSetupCommand 'curl -X GET "https://api.box.com/2.0/users/me?fields=enterprise" -H "Authorization: Bearer ACCESS_TOKEN"' To iSuccess
    Get ComGetVar Of hoCurl "access_token" To sTemp1
    Get ComReplace Of hoSetupCommand "ACCESS_TOKEN" sTemp1 To iSetupReplacements
    Get ComGetAsString Of hoSetupCommand To sTemp1
    Get ComAddFunction Of hoCurl "getEnterprise" sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoCurl To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Register output rules once. Chilkat 11.7.0 evaluates {{name}} variables
    //  when extracting each output, using the current value at that time.
    Get ComAddOutput2 Of hoCurl "findParent" "entries" "name" "{{parent_name}}" True "id" "parent_id" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoCurl To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComAddOutput2 Of hoCurl "findFolder" "entries" "name" "{{folder_name}}" True "id" "folder_id" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoCurl To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComAddOutput2 Of hoCurl "findFile" "entries" "name" "{{file_name}}" True "id" "file_id" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoCurl To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComAddOutput2 Of hoCurl "findDestination" "entries" "name" "{{destination_name}}" True "id" "destination_id" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoCurl To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComAddOutput Of hoCurl "getFileDetails" "file_version.id" "file_version_id" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoCurl To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComAddOutput Of hoCurl "getFileDetails" "sha1" "file_sha1" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoCurl To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComAddOutput Of hoCurl "getEnterprise" "enterprise.id" "enterprise_id" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoCurl To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  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.
    Move "documents" To sParentName
    Get ComGetVar Of hoCurl "parent_name" To sTemp1
    Get ComVarDefined Of hoCurl "parent_id" To bTemp1
    If ((sTemp1 <> sParentName) Or ((sParentName = "/") And (bTemp1 = False))) Begin
        Send ComSetVar To hoCurl "parent_name" sParentName
        Send ComClearVar To hoCurl "parent_id"
        Send ComClearVar To hoCurl "folder_id"
        Send ComClearVar To hoCurl "file_id"
        Send ComClearVar To hoCurl "file_version_id"
        Send ComClearVar To hoCurl "file_sha1"
        If (sParentName = "/") Begin
            Send ComSetVar To hoCurl "parent_id" "0"
        End

    End

    Send ComSetVar To hoCurl "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.
    Send ComClearTargetOutput To hoCurl "*"
    Send ComClearVar To hoCurl "folder_id"
    Send ComAddTargetOutput To hoCurl "id" "folder_id"
    Get Create (RefClass(cComChilkatStringBuilder)) To hoAuthorizedCommand1
    If (Not(IsComObjectCreated(hoAuthorizedCommand1))) Begin
        Send CreateComObject of hoAuthorizedCommand1
    End
    Get ComSetString Of hoAuthorizedCommand1 'curl -X POST "https://api.box.com/2.0/folders" -H "Authorization: Bearer {{access_token}}" -H "Content-Type: application/json" -d '{"name":"{{new_folder_name}}","parent":{"id":"{{parent_id}}"}}'' To iSuccess
    Get ComGetVar Of hoCurl "access_token" To sTemp1
    Get ComReplace Of hoAuthorizedCommand1 "{{access_token}}" sTemp1 To iHeaderReplacement1
    Get ComGetAsString Of hoAuthorizedCommand1 To sTemp1
    Get ComDoYourThing Of hoCurl sTemp1 To iSuccess
    Get ComStatusCode Of hoCurl To iTemp1
    If ((iSuccess <> True) Or (iTemp1 <> 201)) Begin
        Get ComStatusCode Of hoCurl To iTemp1
        Showln "Box request failed. HTTP status: " iTemp1
        Get ComFailReason Of hoCurl To iTemp1
        Showln "Failure reason: " iTemp1
        Get ComResponseBodyStr Of hoCurl To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComVarDefined Of hoCurl "!" To bTemp1
    If (bTemp1 <> True) Begin
        Showln "Expected response fields are missing."
        Procedure_Return
    End

    Get ComGetVar Of hoCurl "folder_id" To sTemp1
    Showln "Created folder: " sTemp1
    //  Resolve the multipart attributes field before passing the command to HttpCurl.
    //  Box folder IDs come from configuration or a previous API response.
    Get Create (RefClass(cComChilkatStringBuilder)) To hoUploadCommand
    If (Not(IsComObjectCreated(hoUploadCommand))) Begin
        Send CreateComObject of hoUploadCommand
    End
    Get Create (RefClass(cComChilkatStringBuilder)) To hoAuthorizedCommand2
    If (Not(IsComObjectCreated(hoAuthorizedCommand2))) Begin
        Send CreateComObject of hoAuthorizedCommand2
    End
    Get ComSetString Of hoAuthorizedCommand2 'curl -X POST "https://upload.box.com/api/2.0/files/content" -H "Authorization: Bearer {{access_token}}" -F 'attributes={"name":"invoice.pdf","parent":{"id":"{{folder_id}}"}}' -F "file=@qa_data/pdf/invoice.pdf;type=application/pdf"' To iSuccess
    Get ComGetVar Of hoCurl "access_token" To sTemp1
    Get ComReplace Of hoAuthorizedCommand2 "{{access_token}}" sTemp1 To iHeaderReplacement2
    Get ComGetAsString Of hoAuthorizedCommand2 To sTemp1
    Get ComSetString Of hoUploadCommand sTemp1 To iSuccess
    Get ComGetVar Of hoCurl "folder_id" To sTemp1
    Get ComReplace Of hoUploadCommand "{{folder_id}}" sTemp1 To iReplacements
    Send ComClearTargetOutput To hoCurl "*"
    Send ComClearVar To hoCurl "file_id"
    Send ComAddTargetOutput To hoCurl "entries[0].id" "file_id"
    Get ComGetAsString Of hoUploadCommand To sTemp1
    Get ComDoYourThing Of hoCurl sTemp1 To iSuccess
    Get ComStatusCode Of hoCurl To iTemp1
    If ((iSuccess <> True) Or (iTemp1 <> 201)) Begin
        Get ComStatusCode Of hoCurl To iTemp1
        Showln "Box request failed. HTTP status: " iTemp1
        Get ComFailReason Of hoCurl To iTemp1
        Showln "Failure reason: " iTemp1
        Get ComResponseBodyStr Of hoCurl To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComVarDefined Of hoCurl "!" To bTemp1
    If (bTemp1 <> True) Begin
        Showln "Expected response fields are missing."
        Procedure_Return
    End

    Get ComGetVar Of hoCurl "file_id" To sTemp1
    Showln "Uploaded file: " sTemp1
    Send ComClearTargetOutput To hoCurl "*"
    Get Create (RefClass(cComChilkatStringBuilder)) To hoAuthorizedCommand3
    If (Not(IsComObjectCreated(hoAuthorizedCommand3))) Begin
        Send CreateComObject of hoAuthorizedCommand3
    End
    Get ComSetString Of hoAuthorizedCommand3 'curl -X POST "https://api.box.com/2.0/files/{{file_id}}/metadata/enterprise_{{enterprise_id}}/projectDocument" -H "Authorization: Bearer {{access_token}}" -H "Content-Type: application/json" -d '{"customerId":"CUST-1001","status":"received"}'' To iSuccess
    Get ComGetVar Of hoCurl "access_token" To sTemp1
    Get ComReplace Of hoAuthorizedCommand3 "{{access_token}}" sTemp1 To iHeaderReplacement3
    Get ComGetAsString Of hoAuthorizedCommand3 To sTemp1
    Get ComDoYourThing Of hoCurl sTemp1 To iSuccess
    Get ComStatusCode Of hoCurl To iTemp1
    If ((iSuccess <> True) Or (iTemp1 <> 201)) Begin
        Get ComStatusCode Of hoCurl To iTemp1
        Showln "Box request failed. HTTP status: " iTemp1
        Get ComFailReason Of hoCurl To iTemp1
        Showln "Failure reason: " iTemp1
        Get ComResponseBodyStr Of hoCurl To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Send ComClearTargetOutput To hoCurl "*"
    Send ComClearVar To hoCurl "shared_url"
    Send ComAddTargetOutput To hoCurl "shared_link.url" "shared_url"
    Get Create (RefClass(cComChilkatStringBuilder)) To hoAuthorizedCommand4
    If (Not(IsComObjectCreated(hoAuthorizedCommand4))) Begin
        Send CreateComObject of hoAuthorizedCommand4
    End
    Get ComSetString Of hoAuthorizedCommand4 'curl -X PUT "https://api.box.com/2.0/files/{{file_id}}?fields=id,shared_link" -H "Authorization: Bearer {{access_token}}" -H "Content-Type: application/json" -d '{"shared_link":{"access":"collaborators"}}'' To iSuccess
    Get ComGetVar Of hoCurl "access_token" To sTemp1
    Get ComReplace Of hoAuthorizedCommand4 "{{access_token}}" sTemp1 To iHeaderReplacement4
    Get ComGetAsString Of hoAuthorizedCommand4 To sTemp1
    Get ComDoYourThing Of hoCurl sTemp1 To iSuccess
    Get ComStatusCode Of hoCurl To iTemp1
    If ((iSuccess <> True) Or (iTemp1 <> 200)) Begin
        Get ComStatusCode Of hoCurl To iTemp1
        Showln "Box request failed. HTTP status: " iTemp1
        Get ComFailReason Of hoCurl To iTemp1
        Showln "Failure reason: " iTemp1
        Get ComResponseBodyStr Of hoCurl To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComVarDefined Of hoCurl "!" To bTemp1
    If (bTemp1 <> True) Begin
        Showln "Expected response fields are missing."
        Procedure_Return
    End

    Get ComGetVar Of hoCurl "shared_url" To sTemp1
    Showln "Document link: " sTemp1
    //  The link is for existing collaborators. Invite users separately when needed.
    //  ClearTargetOutput removes old extraction rules; it does not clear existing IDs.
    Get ComGetVar Of hoCurl "new_folder_name" To sTemp1
    Send ComSetVar To hoCurl "folder_name" sTemp1
    Send ComSetVar To hoCurl "file_name" "invoice.pdf"
    Send ComClearVar To hoCurl "file_version_id"
    Send ComClearVar To hoCurl "file_sha1"


End_Procedure