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

Box: Create a Folder and Capture Its ID

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. Create Project Documents under the root child named documents. The new folder ID is captured for later operations; conflicts stop the example. Set parent_name to / to create directly under root.

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

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

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

    success = false

    //  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.
    let curl = CkoHttpCurl()!
    curl.setVar(varName: "access_token", varValue: "BOX_ACCESS_TOKEN")
    curl.setVar(varName: "parent_name", varValue: "/")
    //  Box's fixed root ID is an implementation detail; callers supply names.
    curl.setVar(varName: "parent_id", varValue: "0")
    let setupCommand = CkoStringBuilder()!
    var setupReplacements: Int

    //  Register findParent; registration sends no request.
    setupCommand.setString(value: "curl -X GET \"https://api.box.com/2.0/folders/0/items?limit=1000&fields=id,type,name\" -H \"Authorization: Bearer ACCESS_TOKEN\"")
    setupReplacements = setupCommand.replace(value: "ACCESS_TOKEN", replacement: curl.getVar(varName: "access_token")).intValue
    success = curl.addFunction(funcName: "findParent", curl: setupCommand.getAsString())
    if success != true {
        print("\(curl.lastErrorText!)")
        return
    }

    //  Register findFolder; registration sends no request.
    setupCommand.setString(value: "curl -X GET \"https://api.box.com/2.0/folders/{{parent_id}}/items?limit=1000&fields=id,type,name\" -H \"Authorization: Bearer ACCESS_TOKEN\"")
    setupReplacements = setupCommand.replace(value: "ACCESS_TOKEN", replacement: curl.getVar(varName: "access_token")).intValue
    success = curl.addFunction(funcName: "findFolder", curl: setupCommand.getAsString())
    if success != true {
        print("\(curl.lastErrorText!)")
        return
    }

    //  Register findFile; registration sends no request.
    setupCommand.setString(value: "curl -X GET \"https://api.box.com/2.0/folders/{{folder_id}}/items?limit=1000&fields=id,type,name\" -H \"Authorization: Bearer ACCESS_TOKEN\"")
    setupReplacements = setupCommand.replace(value: "ACCESS_TOKEN", replacement: curl.getVar(varName: "access_token")).intValue
    success = curl.addFunction(funcName: "findFile", curl: setupCommand.getAsString())
    if success != true {
        print("\(curl.lastErrorText!)")
        return
    }

    //  Register findDestination; registration sends no request.
    setupCommand.setString(value: "curl -X GET \"https://api.box.com/2.0/folders/0/items?limit=1000&fields=id,type,name\" -H \"Authorization: Bearer ACCESS_TOKEN\"")
    setupReplacements = setupCommand.replace(value: "ACCESS_TOKEN", replacement: curl.getVar(varName: "access_token")).intValue
    success = curl.addFunction(funcName: "findDestination", curl: setupCommand.getAsString())
    if success != true {
        print("\(curl.lastErrorText!)")
        return
    }

    //  Register getFileDetails; registration sends no request.
    setupCommand.setString(value: "curl -X GET \"https://api.box.com/2.0/files/{{file_id}}?fields=id,sha1,file_version\" -H \"Authorization: Bearer ACCESS_TOKEN\"")
    setupReplacements = setupCommand.replace(value: "ACCESS_TOKEN", replacement: curl.getVar(varName: "access_token")).intValue
    success = curl.addFunction(funcName: "getFileDetails", curl: setupCommand.getAsString())
    if success != true {
        print("\(curl.lastErrorText!)")
        return
    }

    //  Register getEnterprise; registration sends no request.
    setupCommand.setString(value: "curl -X GET \"https://api.box.com/2.0/users/me?fields=enterprise\" -H \"Authorization: Bearer ACCESS_TOKEN\"")
    setupReplacements = setupCommand.replace(value: "ACCESS_TOKEN", replacement: curl.getVar(varName: "access_token")).intValue
    success = curl.addFunction(funcName: "getEnterprise", curl: setupCommand.getAsString())
    if success != true {
        print("\(curl.lastErrorText!)")
        return
    }

    //  Register output rules once. Chilkat 11.7.0 evaluates {{name}} variables
    //  when extracting each output, using the current value at that time.
    success = curl.addOutput2(funcName: "findParent", arrayPath: "entries", wherePath: "name", whereValue: "{{parent_name}}", caseSensitive: true, subPath: "id", varName: "parent_id")
    if success != true {
        print("\(curl.lastErrorText!)")
        return
    }

    success = curl.addOutput2(funcName: "findFolder", arrayPath: "entries", wherePath: "name", whereValue: "{{folder_name}}", caseSensitive: true, subPath: "id", varName: "folder_id")
    if success != true {
        print("\(curl.lastErrorText!)")
        return
    }

    success = curl.addOutput2(funcName: "findFile", arrayPath: "entries", wherePath: "name", whereValue: "{{file_name}}", caseSensitive: true, subPath: "id", varName: "file_id")
    if success != true {
        print("\(curl.lastErrorText!)")
        return
    }

    success = curl.addOutput2(funcName: "findDestination", arrayPath: "entries", wherePath: "name", whereValue: "{{destination_name}}", caseSensitive: true, subPath: "id", varName: "destination_id")
    if success != true {
        print("\(curl.lastErrorText!)")
        return
    }

    success = curl.addOutput(funcName: "getFileDetails", jsonPath: "file_version.id", varName: "file_version_id")
    if success != true {
        print("\(curl.lastErrorText!)")
        return
    }

    success = curl.addOutput(funcName: "getFileDetails", jsonPath: "sha1", varName: "file_sha1")
    if success != true {
        print("\(curl.lastErrorText!)")
        return
    }

    success = curl.addOutput(funcName: "getEnterprise", jsonPath: "enterprise.id", varName: "enterprise_id")
    if success != true {
        print("\(curl.lastErrorText!)")
        return
    }

    //  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.
    var parentName: String? = "documents"
    if (curl.getVar(varName: "parent_name") != parentName) || ((parentName == "/") && (curl.varDefined(varName: "parent_id") == false)) {
        curl.setVar(varName: "parent_name", varValue: parentName)
        curl.clearVar(varName: "parent_id")
        curl.clearVar(varName: "folder_id")
        curl.clearVar(varName: "file_id")
        curl.clearVar(varName: "file_version_id")
        curl.clearVar(varName: "file_sha1")
        if parentName == "/" {
            curl.setVar(varName: "parent_id", varValue: "0")
        }

    }

    curl.setVar(varName: "new_folder_name", varValue: "Project Documents")
    curl.clearTargetOutput(varName: "*")
    curl.clearVar(varName: "folder_id")
    curl.addTargetOutput(jsonPath: "id", varName: "folder_id")
    let authorizedCommand1 = CkoStringBuilder()!
    authorizedCommand1.setString(value: "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}}\"}}'")
    var headerReplacement1: Int = authorizedCommand1.replace(value: "{{access_token}}", replacement: curl.getVar(varName: "access_token")).intValue
    success = curl.doYourThing(targetCurl: authorizedCommand1.getAsString())
    if (success != true) || (curl.statusCode.intValue != 201) {
        print("Box request failed. HTTP status: \(curl.statusCode.intValue)")
        print("Failure reason: \(curl.failReason.intValue)")
        print("\(curl.responseBodyStr!)")
        return
    }

    if curl.varDefined(varName: "!") != true {
        print("Expected response fields are missing.")
        return
    }

    print("Created folder ID: \(curl.getVar(varName: "folder_id")!)")
    curl.setVar(varName: "folder_name", varValue: curl.getVar(varName: "new_folder_name"))

}