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

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Curl
oleobject loo_SetupCommand
long li_SetupReplacements
string ls_ParentName
oleobject loo_AuthorizedCommand1
long li_HeaderReplacement1
oleobject loo_UploadCommand
oleobject loo_AuthorizedCommand2
long li_HeaderReplacement2
long li_Replacements
oleobject loo_AuthorizedCommand3
long li_HeaderReplacement3
oleobject loo_AuthorizedCommand4
long li_HeaderReplacement4

li_Success = 0

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

li_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.
loo_Curl = create oleobject
li_rc = loo_Curl.ConnectToNewObject("Chilkat.HttpCurl")
if li_rc < 0 then
    destroy loo_Curl
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Curl.SetVar("access_token","BOX_ACCESS_TOKEN")
loo_Curl.SetVar("parent_name","/")
//  Box's fixed root ID is an implementation detail; callers supply names.
loo_Curl.SetVar("parent_id","0")
loo_SetupCommand = create oleobject
li_rc = loo_SetupCommand.ConnectToNewObject("Chilkat.StringBuilder")

//  Register findParent; registration sends no request.
loo_SetupCommand.SetString("curl -X GET ~"https://api.box.com/2.0/folders/0/items?limit=1000&fields=id,type,name~" -H ~"Authorization: Bearer ACCESS_TOKEN~"")
li_SetupReplacements = loo_SetupCommand.Replace("ACCESS_TOKEN",loo_Curl.GetVar("access_token"))
li_Success = loo_Curl.AddFunction("findParent",loo_SetupCommand.GetAsString())
if li_Success <> 1 then
    Write-Debug loo_Curl.LastErrorText
    destroy loo_Curl
    destroy loo_SetupCommand
    return
end if

//  Register findFolder; registration sends no request.
loo_SetupCommand.SetString("curl -X GET ~"https://api.box.com/2.0/folders/{{parent_id}}/items?limit=1000&fields=id,type,name~" -H ~"Authorization: Bearer ACCESS_TOKEN~"")
li_SetupReplacements = loo_SetupCommand.Replace("ACCESS_TOKEN",loo_Curl.GetVar("access_token"))
li_Success = loo_Curl.AddFunction("findFolder",loo_SetupCommand.GetAsString())
if li_Success <> 1 then
    Write-Debug loo_Curl.LastErrorText
    destroy loo_Curl
    destroy loo_SetupCommand
    return
end if

//  Register findFile; registration sends no request.
loo_SetupCommand.SetString("curl -X GET ~"https://api.box.com/2.0/folders/{{folder_id}}/items?limit=1000&fields=id,type,name~" -H ~"Authorization: Bearer ACCESS_TOKEN~"")
li_SetupReplacements = loo_SetupCommand.Replace("ACCESS_TOKEN",loo_Curl.GetVar("access_token"))
li_Success = loo_Curl.AddFunction("findFile",loo_SetupCommand.GetAsString())
if li_Success <> 1 then
    Write-Debug loo_Curl.LastErrorText
    destroy loo_Curl
    destroy loo_SetupCommand
    return
end if

//  Register findDestination; registration sends no request.
loo_SetupCommand.SetString("curl -X GET ~"https://api.box.com/2.0/folders/0/items?limit=1000&fields=id,type,name~" -H ~"Authorization: Bearer ACCESS_TOKEN~"")
li_SetupReplacements = loo_SetupCommand.Replace("ACCESS_TOKEN",loo_Curl.GetVar("access_token"))
li_Success = loo_Curl.AddFunction("findDestination",loo_SetupCommand.GetAsString())
if li_Success <> 1 then
    Write-Debug loo_Curl.LastErrorText
    destroy loo_Curl
    destroy loo_SetupCommand
    return
end if

//  Register getFileDetails; registration sends no request.
loo_SetupCommand.SetString("curl -X GET ~"https://api.box.com/2.0/files/{{file_id}}?fields=id,sha1,file_version~" -H ~"Authorization: Bearer ACCESS_TOKEN~"")
li_SetupReplacements = loo_SetupCommand.Replace("ACCESS_TOKEN",loo_Curl.GetVar("access_token"))
li_Success = loo_Curl.AddFunction("getFileDetails",loo_SetupCommand.GetAsString())
if li_Success <> 1 then
    Write-Debug loo_Curl.LastErrorText
    destroy loo_Curl
    destroy loo_SetupCommand
    return
end if

//  Register getEnterprise; registration sends no request.
loo_SetupCommand.SetString("curl -X GET ~"https://api.box.com/2.0/users/me?fields=enterprise~" -H ~"Authorization: Bearer ACCESS_TOKEN~"")
li_SetupReplacements = loo_SetupCommand.Replace("ACCESS_TOKEN",loo_Curl.GetVar("access_token"))
li_Success = loo_Curl.AddFunction("getEnterprise",loo_SetupCommand.GetAsString())
if li_Success <> 1 then
    Write-Debug loo_Curl.LastErrorText
    destroy loo_Curl
    destroy loo_SetupCommand
    return
end if

//  Register output rules once. Chilkat 11.7.0 evaluates {{name}} variables
//  when extracting each output, using the current value at that time.
li_Success = loo_Curl.AddOutput2("findParent","entries","name","{{parent_name}}",1,"id","parent_id")
if li_Success <> 1 then
    Write-Debug loo_Curl.LastErrorText
    destroy loo_Curl
    destroy loo_SetupCommand
    return
end if

li_Success = loo_Curl.AddOutput2("findFolder","entries","name","{{folder_name}}",1,"id","folder_id")
if li_Success <> 1 then
    Write-Debug loo_Curl.LastErrorText
    destroy loo_Curl
    destroy loo_SetupCommand
    return
end if

li_Success = loo_Curl.AddOutput2("findFile","entries","name","{{file_name}}",1,"id","file_id")
if li_Success <> 1 then
    Write-Debug loo_Curl.LastErrorText
    destroy loo_Curl
    destroy loo_SetupCommand
    return
end if

li_Success = loo_Curl.AddOutput2("findDestination","entries","name","{{destination_name}}",1,"id","destination_id")
if li_Success <> 1 then
    Write-Debug loo_Curl.LastErrorText
    destroy loo_Curl
    destroy loo_SetupCommand
    return
end if

li_Success = loo_Curl.AddOutput("getFileDetails","file_version.id","file_version_id")
if li_Success <> 1 then
    Write-Debug loo_Curl.LastErrorText
    destroy loo_Curl
    destroy loo_SetupCommand
    return
end if

li_Success = loo_Curl.AddOutput("getFileDetails","sha1","file_sha1")
if li_Success <> 1 then
    Write-Debug loo_Curl.LastErrorText
    destroy loo_Curl
    destroy loo_SetupCommand
    return
end if

li_Success = loo_Curl.AddOutput("getEnterprise","enterprise.id","enterprise_id")
if li_Success <> 1 then
    Write-Debug loo_Curl.LastErrorText
    destroy loo_Curl
    destroy loo_SetupCommand
    return
end if

//  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.
ls_ParentName = "documents"
if (loo_Curl.GetVar("parent_name") <> ls_ParentName) OR ((ls_ParentName = "/") AND (loo_Curl.VarDefined("parent_id") = 0)) then
    loo_Curl.SetVar("parent_name",ls_ParentName)
    loo_Curl.ClearVar("parent_id")
    loo_Curl.ClearVar("folder_id")
    loo_Curl.ClearVar("file_id")
    loo_Curl.ClearVar("file_version_id")
    loo_Curl.ClearVar("file_sha1")
    if ls_ParentName = "/" then
        loo_Curl.SetVar("parent_id","0")
    end if

end if

loo_Curl.SetVar("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.
loo_Curl.ClearTargetOutput("*")
loo_Curl.ClearVar("folder_id")
loo_Curl.AddTargetOutput("id","folder_id")
loo_AuthorizedCommand1 = create oleobject
li_rc = loo_AuthorizedCommand1.ConnectToNewObject("Chilkat.StringBuilder")

loo_AuthorizedCommand1.SetString("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}}~"}}'")
li_HeaderReplacement1 = loo_AuthorizedCommand1.Replace("{{access_token}}",loo_Curl.GetVar("access_token"))
li_Success = loo_Curl.DoYourThing(loo_AuthorizedCommand1.GetAsString())
if (li_Success <> 1) OR (loo_Curl.StatusCode <> 201) then
    Write-Debug "Box request failed. HTTP status: " + string(loo_Curl.StatusCode)
    Write-Debug "Failure reason: " + string(loo_Curl.FailReason)
    Write-Debug loo_Curl.ResponseBodyStr
    destroy loo_Curl
    destroy loo_SetupCommand
    destroy loo_AuthorizedCommand1
    return
end if

if loo_Curl.VarDefined("!") <> 1 then
    Write-Debug "Expected response fields are missing."
    destroy loo_Curl
    destroy loo_SetupCommand
    destroy loo_AuthorizedCommand1
    return
end if

Write-Debug "Created folder: " + loo_Curl.GetVar("folder_id")
//  Resolve the multipart attributes field before passing the command to HttpCurl.
//  Box folder IDs come from configuration or a previous API response.
loo_UploadCommand = create oleobject
li_rc = loo_UploadCommand.ConnectToNewObject("Chilkat.StringBuilder")

loo_AuthorizedCommand2 = create oleobject
li_rc = loo_AuthorizedCommand2.ConnectToNewObject("Chilkat.StringBuilder")

loo_AuthorizedCommand2.SetString("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~"")
li_HeaderReplacement2 = loo_AuthorizedCommand2.Replace("{{access_token}}",loo_Curl.GetVar("access_token"))
loo_UploadCommand.SetString(loo_AuthorizedCommand2.GetAsString())
li_Replacements = loo_UploadCommand.Replace("{{folder_id}}",loo_Curl.GetVar("folder_id"))
loo_Curl.ClearTargetOutput("*")
loo_Curl.ClearVar("file_id")
loo_Curl.AddTargetOutput("entries[0].id","file_id")
li_Success = loo_Curl.DoYourThing(loo_UploadCommand.GetAsString())
if (li_Success <> 1) OR (loo_Curl.StatusCode <> 201) then
    Write-Debug "Box request failed. HTTP status: " + string(loo_Curl.StatusCode)
    Write-Debug "Failure reason: " + string(loo_Curl.FailReason)
    Write-Debug loo_Curl.ResponseBodyStr
    destroy loo_Curl
    destroy loo_SetupCommand
    destroy loo_AuthorizedCommand1
    destroy loo_UploadCommand
    destroy loo_AuthorizedCommand2
    return
end if

if loo_Curl.VarDefined("!") <> 1 then
    Write-Debug "Expected response fields are missing."
    destroy loo_Curl
    destroy loo_SetupCommand
    destroy loo_AuthorizedCommand1
    destroy loo_UploadCommand
    destroy loo_AuthorizedCommand2
    return
end if

Write-Debug "Uploaded file: " + loo_Curl.GetVar("file_id")
loo_Curl.ClearTargetOutput("*")
loo_AuthorizedCommand3 = create oleobject
li_rc = loo_AuthorizedCommand3.ConnectToNewObject("Chilkat.StringBuilder")

loo_AuthorizedCommand3.SetString("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~"}'")
li_HeaderReplacement3 = loo_AuthorizedCommand3.Replace("{{access_token}}",loo_Curl.GetVar("access_token"))
li_Success = loo_Curl.DoYourThing(loo_AuthorizedCommand3.GetAsString())
if (li_Success <> 1) OR (loo_Curl.StatusCode <> 201) then
    Write-Debug "Box request failed. HTTP status: " + string(loo_Curl.StatusCode)
    Write-Debug "Failure reason: " + string(loo_Curl.FailReason)
    Write-Debug loo_Curl.ResponseBodyStr
    destroy loo_Curl
    destroy loo_SetupCommand
    destroy loo_AuthorizedCommand1
    destroy loo_UploadCommand
    destroy loo_AuthorizedCommand2
    destroy loo_AuthorizedCommand3
    return
end if

loo_Curl.ClearTargetOutput("*")
loo_Curl.ClearVar("shared_url")
loo_Curl.AddTargetOutput("shared_link.url","shared_url")
loo_AuthorizedCommand4 = create oleobject
li_rc = loo_AuthorizedCommand4.ConnectToNewObject("Chilkat.StringBuilder")

loo_AuthorizedCommand4.SetString("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~"}}'")
li_HeaderReplacement4 = loo_AuthorizedCommand4.Replace("{{access_token}}",loo_Curl.GetVar("access_token"))
li_Success = loo_Curl.DoYourThing(loo_AuthorizedCommand4.GetAsString())
if (li_Success <> 1) OR (loo_Curl.StatusCode <> 200) then
    Write-Debug "Box request failed. HTTP status: " + string(loo_Curl.StatusCode)
    Write-Debug "Failure reason: " + string(loo_Curl.FailReason)
    Write-Debug loo_Curl.ResponseBodyStr
    destroy loo_Curl
    destroy loo_SetupCommand
    destroy loo_AuthorizedCommand1
    destroy loo_UploadCommand
    destroy loo_AuthorizedCommand2
    destroy loo_AuthorizedCommand3
    destroy loo_AuthorizedCommand4
    return
end if

if loo_Curl.VarDefined("!") <> 1 then
    Write-Debug "Expected response fields are missing."
    destroy loo_Curl
    destroy loo_SetupCommand
    destroy loo_AuthorizedCommand1
    destroy loo_UploadCommand
    destroy loo_AuthorizedCommand2
    destroy loo_AuthorizedCommand3
    destroy loo_AuthorizedCommand4
    return
end if

Write-Debug "Document link: " + loo_Curl.GetVar("shared_url")
//  The link is for existing collaborators. Invite users separately when needed.
//  ClearTargetOutput removes old extraction rules; it does not clear existing IDs.
loo_Curl.SetVar("folder_name",loo_Curl.GetVar("new_folder_name"))
loo_Curl.SetVar("file_name","invoice.pdf")
loo_Curl.ClearVar("file_version_id")
loo_Curl.ClearVar("file_sha1")


destroy loo_Curl
destroy loo_SetupCommand
destroy loo_AuthorizedCommand1
destroy loo_UploadCommand
destroy loo_AuthorizedCommand2
destroy loo_AuthorizedCommand3
destroy loo_AuthorizedCommand4