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

Box: Resolve Folder and File IDs with the HttpCurl Dependency Engine

See more Box Examples

Download images/penguins.jpg using search-based HttpCurl dependencies and one DoYourThing call. Register search requests/output rules once, then supply names. Search results must each contain exactly one item; exact names and immediate parent IDs are checked before saving the downloaded bytes. The candidate content is downloaded into memory before validation, so this example is for small files. Multiple results are rejected instead of paginated or silently choosing a candidate. Search indexing can delay visibility of newly created or renamed items.

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

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

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

local success = false

-- BEGIN ONE-TIME SETUP
-- Requires Chilkat 11.7.0. Registration sends no requests.
-- Run this once and keep the SAME HttpCurl object for later downloads.
-- This focused setup searches for a root folder and a file directly inside it.
local curl = chilkat.HttpCurl.new()
curl:set_var("access_token", "BOX_ACCESS_TOKEN")
local setupCommand = chilkat.StringBuilder.new()

-- searchFolder: Box searches names; quotes request a phrase match.
setupCommand:set_string("curl -X GET \"https://api.box.com/2.0/search?query={{folder_query}}&type=folder&content_types=name&fields=id,type,name,parent&limit=2\" -H \"Authorization: Bearer ACCESS_TOKEN\"")
local setupReplacements = setupCommand:replace("ACCESS_TOKEN", curl:get_var("access_token"))
success = curl:add_function("searchFolder", setupCommand:get_as_string())
if not success then
    print(curl.last_error_text)
    return
end

-- Deferred selection: folder_name need not be set during setup.
success = curl:add_output2("searchFolder", "entries", "name", "{{folder_name}}", true, "id", "folder_id")
if not success then
    print(curl.last_error_text)
    return
end

success = curl:add_output2("searchFolder", "entries", "name", "{{folder_name}}", true, "name", "folder_result_name")
if not success then
    print(curl.last_error_text)
    return
end

success = curl:add_output2("searchFolder", "entries", "name", "{{folder_name}}", true, "parent.id", "folder_parent_id")
if not success then
    print(curl.last_error_text)
    return
end

success = curl:add_output("searchFolder", "total_count", "folder_search_count")
if not success then
    print(curl.last_error_text)
    return
end

-- searchFile: Box searches names; quotes request a phrase match.
setupCommand:set_string("curl -X GET \"https://api.box.com/2.0/search?query={{file_query}}&type=file&content_types=name&fields=id,type,name,parent&limit=2&ancestor_folder_ids={{folder_id}}\" -H \"Authorization: Bearer ACCESS_TOKEN\"")
setupReplacements = setupCommand:replace("ACCESS_TOKEN", curl:get_var("access_token"))
success = curl:add_function("searchFile", setupCommand:get_as_string())
if not success then
    print(curl.last_error_text)
    return
end

-- Deferred selection: file_name need not be set during setup.
success = curl:add_output2("searchFile", "entries", "name", "{{file_name}}", true, "id", "file_id")
if not success then
    print(curl.last_error_text)
    return
end

success = curl:add_output2("searchFile", "entries", "name", "{{file_name}}", true, "name", "file_result_name")
if not success then
    print(curl.last_error_text)
    return
end

success = curl:add_output2("searchFile", "entries", "name", "{{file_name}}", true, "parent.id", "file_parent_id")
if not success then
    print(curl.last_error_text)
    return
end

success = curl:add_output("searchFile", "total_count", "file_search_count")
if not success then
    print(curl.last_error_text)
    return
end

-- END ONE-TIME SETUP
-- Search is indexed: newly uploaded/renamed items may not appear immediately.
-- Search matches phrases, not exact strings, and ancestor filtering includes
-- subfolders. The operation validates counts, exact names, and immediate parents.
-- limit=2 is enough to demonstrate an ambiguous search; total_count covers all
-- results, not just this page. This introductory example requires exactly one
-- search result at each step and does not select among multiple candidates.

-- BEGIN OPERATION -- use the previously configured object.
-- Human-readable inputs. No resource IDs are supplied by the caller.
local folderName = "images"
local fileName = "penguins.jpg"
-- Encode the quoted search queries separately from the literal output filters.
-- In this build, URL variable substitution does not encode '&' in a name.
local queryText = chilkat.StringBuilder.new()
queryText:set_string("\"")
queryText:append(folderName)
queryText:append("\"")
curl:set_var("folder_query", queryText:get_encoded("url", "utf-8"))
queryText:set_string("\"")
queryText:append(fileName)
queryText:append("\"")
curl:set_var("file_query", queryText:get_encoded("url", "utf-8"))
if curl:get_var("folder_name") ~= folderName then
    curl:clear_var("folder_id")
    curl:clear_var("folder_result_name")
    curl:clear_var("folder_parent_id")
    curl:clear_var("folder_search_count")
    curl:clear_var("file_id")
    curl:clear_var("file_result_name")
    curl:clear_var("file_parent_id")
    curl:clear_var("file_search_count")
    curl:set_var("folder_name", folderName)
end

if curl:get_var("file_name") ~= fileName then
    curl:clear_var("file_id")
    curl:clear_var("file_result_name")
    curl:clear_var("file_parent_id")
    curl:clear_var("file_search_count")
    curl:set_var("file_name", fileName)
end

-- With unchanged names, cached IDs and validation fields can be reused.
-- Clear the corresponding cached fields to refresh after a remote rename/move.
local localPath = "qa_output/penguins.jpg"
-- A cold plan is searchFolder -> searchFile -> targetCurl.
-- No listing endpoint or manual search call is used.
local plan = chilkat.JsonObject.new()
plan.emit_compact = false
local authorizedCommand1 = chilkat.StringBuilder.new()
authorizedCommand1:set_string("curl -X GET \"https://api.box.com/2.0/files/{{file_id}}/content\" -H \"Authorization: Bearer {{access_token}}\" -L")
local headerReplacement1 = authorizedCommand1:replace("{{access_token}}", curl:get_var("access_token"))
success = curl:examine_plan(authorizedCommand1:get_as_string(), plan)
if not success then
    print(curl.last_error_text)
    return
end

print(plan:emit())
-- Keep the candidate download in memory; do not write an unvalidated result.
curl.response_file_path = ""
curl:clear_target_output("*")
local authorizedCommand2 = chilkat.StringBuilder.new()
authorizedCommand2:set_string("curl -X GET \"https://api.box.com/2.0/files/{{file_id}}/content\" -H \"Authorization: Bearer {{access_token}}\" -L")
local headerReplacement2 = authorizedCommand2:replace("{{access_token}}", curl:get_var("access_token"))
success = curl:do_your_thing(authorizedCommand2:get_as_string())
if (not success) or (curl.status_code ~= 200) then
    print("Box request failed. HTTP status: " .. curl.status_code)
    print("Failure reason: " .. curl.fail_reason)
    return
end

-- AddOutput2 supplies candidate IDs. Box's phrase search and recursive
-- ancestor filter are not an exact path lookup, so validate before saving.
if (curl:get_var("folder_search_count") ~= "1") or (curl:get_var("file_search_count") ~= "1") then
    print("Search was ambiguous or incomplete. Expected exactly one result per search; no file was saved.")
    print("Folder results: " .. curl:get_var("folder_search_count") .. " File results: " .. curl:get_var("file_search_count"))
    curl:clear_var("folder_id")
    curl:clear_var("folder_result_name")
    curl:clear_var("folder_parent_id")
    curl:clear_var("folder_search_count")
    curl:clear_var("file_id")
    curl:clear_var("file_result_name")
    curl:clear_var("file_parent_id")
    curl:clear_var("file_search_count")
    return
end

if (curl:get_var("folder_result_name") ~= folderName) or ((curl:get_var("folder_parent_id") ~= "0") or ((curl:get_var("file_result_name") ~= fileName) or (curl:get_var("file_parent_id") ~= curl:get_var("folder_id")))) then
    print("The search result is not the exact root-folder/filename requested; no file was saved.")
    curl:clear_var("folder_id")
    curl:clear_var("folder_result_name")
    curl:clear_var("folder_parent_id")
    curl:clear_var("folder_search_count")
    curl:clear_var("file_id")
    curl:clear_var("file_result_name")
    curl:clear_var("file_parent_id")
    curl:clear_var("file_search_count")
    return
end

-- Validation is after the network download but before writing the destination.
-- This in-memory example is intended for small files such as penguins.jpg.
-- Create qa_output before running it.
local contents = chilkat.BinData.new()
success = curl:get_response_bd(contents)
if not success then
    print(curl.last_error_text)
    return
end

success = contents:write_file(localPath)
if not success then
    print(contents.last_error_text)
    return
end

print("Downloaded to " .. localPath)