File uploads with the Upload API
With Builder's Upload API, developers can programmatically upload files such as images, PDFs, and videos.
Developers can use the Upload API to automate the asset upload process, saving time and reducing manual effort.
Prerequisites
- Create a Private API Key in your Organization Account Settings.
- Review Managing Private API Keys to keep your key secure. Note that only those with Developer or Admin permissions can create Private API Keys.
Upload API endpoint
The endpoint supports the following query parameters:
&name: specifies the name of the uploaded file.&altText: provides alternative text for accessibility and SEO.&title: sets a default title for the uploaded asset.&folder: uploads the file to a specific folder in the Asset Library using the folder ID.&url: uploads a file from a hosted URL instead of directly uploading the file content.
To upload a file, replace the following placeholders in the API request with the actual filename, alt text, title, and folder ID:
ASSET_NAMEASSET_ALT_TEXTASSET_TITLEASSET_FOLDER_ID
Steps to upload a file
- Copy your Private API Key.
- Make a
POSTrequest to the endpoint with the filename as a parameter for your file. AddaltTextandtitlefor accessibility. - Provide the path in the request body. The file must be under 100 MB.
- Include the Authorization header with your Private API Key.
- Specify the file type in the
Content-Typeheader.
A successful POST request returns a JSON response with the URL of the uploaded file.
POST request
To observe the file upload process, send curl request with name, altText, and title parameters, along with the following headers to upload an image:
--data-binaryin the request body to specify the file path.Authorizationheader with your Private API Key.Content-Typeheader to define the file MIME type.
JSON Response
A successful upload returns a JSON response containing details about the uploaded file, including its URL.
You can also set the name, alt text, and title for the files from the Asset library.
Upload API in different environments
Example of Upload API in Node runtime to upload an image
- Use the Node
fsmodule to read the file from disk with thefs.readFileSync(filePath)function. - Assign the resulting binary data of the file to a variable.
- Specify the upload URL with query parameters with
nameandaltText. - Use
fetch()to send aPOSTrequest to the endpoint, passing theURLas the first parameter and therequestObjectas the second parameter. - Set the method type to
POSTand include theAuthorizationandContent-Typeheaders in the object. - Parse the JSON from the
fetch()request and output the console.
Example of Upload API in browser runtime to upload a PDF file
- Use an
<input>tag to upload the file. Assign the binary data of the selected file to a variable. - Create a
Headers()object and add theAuthorizationtoken andContent-Typeto the headers. - Define
requestOptionswith thePOSTmethod,headers, andbodycontaining the file data. - Send a
fetch()request withrequestOptionsto the upload endpoint as anURL. - Convert the resolved response from the
fetch()request totext. - Log the response to the console.
Example of Upload API to upload a file from a URL
- Specify the upload URL with query parameters for url ,
nameandaltText. - Use
fetch()to send aPOSTrequest to the endpoint, passing theURLas the first parameter and therequestObjectas the second parameter. - Set the method type to
POSTand include theAuthorizationandContent-Typeheaders in the object. - Parse the JSON from the
fetch()request and output the console.
Upload to a destination asset folder
Tip: If you are using environments, you can manage folders only in the main environment. Builder syncs any updates made to folders in the main environment with child environments
To modify folders, use your main environment API Key.
For more information, visit Understanding Environments.
If you're using folders to organize assets in Builder, you can upload to a specific folder by adding the folder ID to the URL to the folder query parameter.
For example, to upload an asset to a folder with the ID abc123, add &folder=abc123 to the URL, replacing abc123 with your actual folder ID.
To find an asset folder ID:
- Go to the Asset Library.
- Hover over the folder and click the Pencil icon.
- In the dialogue that opens, copy the ID.
For example, if the ID were a0c7e097c0c34c16aadc5a204affe346, the syntax would be:
The following image shows the Edit Asset Folder dialogue, with the folder ID highlighted: