1
Create and configure a Box application
The first step for any Box Platform integration is to create and configure a Box
application.
- Go to Box Developer Console.
- For this quick start, create an App with the
Client Credentials Grantapplication type. - Once the app is created, enable the following scopes:
Read all files and folders stored in BoxWrite all files and folders stored in BoxManage AI
2
Create a Box metadata template
Box AI enables you to extract data from documents in several ways:
For this quick start, create a Box metadata template to define the fields you want
to extract from your documents. See Customizing Metadata Templates
for a detailed walkthrough of the steps to create a Box metadata template in the Box Admin
Console.
| Type | Description | Use case |
|---|---|---|
| Freeform extraction | Accepts a string prompt. | Provide a natural language prompt. |
| Structured extraction with template | Accepts a Box Metadata template key. | Define fields and data types once; simplifies pushing back to Box as metadata. |
| Structured extraction with fields | Accepts a JSON array of fields. | Run one-off extractions without creating a template. |
| Enhanced Extract Agent | Uses a specialized agent with a reasoning model. | Use for complex documents or nuanced extraction; works with structured templates or fields. |
-
Give your template a name, for example,
Box AI extract quick start. -
Create the following fields:
Field name Type Description Client Name Text The name of the client receiving the invoice Invoice Amount Number The total amount of the invoice after taxes and fees Products Text The names of the products delivered in the invoice, returned as a comma-delimited string - Click Save to create your template. Make note of the template key to use in a later step.
https://app.box.com/master/metadata/templates/boxAiExtractquick start.In this case, the template key is boxAiExtractquick start.3
Upload a test file
After preparing the template, select a file to test. For this quick start, use this
sample invoice document.
- Download the test document, and then drag and drop it into your Box account.
-
Get the file ID by opening the file in Box and inspecting the URL.
The last part of the path is your file ID. For example, the URL might look like this:
https://app.box.com/file/2064123286902In this case, the file ID is2064123286902.
4
Configure the environment
Now set up your development environment to run the extraction. For this quick start, use Python and the
Box Python SDK version 10. Make sure you have Python 3.11 or higher installed on your machine.
- Create a new directory for your project and navigate into it.
- Create a virtual environment:
- Install the Box Python SDK:
- Install the
python-dotenvpackage to load environment variables from the.envfile: - Create an
.envfile in the root of your project directory and add the following environment variables, replacing the placeholder values with your actual Box app credentials and the IDs from the previous steps:
- Click Generate Developer Token to create a new token.
5
Create the extract.py file
Your development environment is now ready to create the Python script to extract data from the document using Box AI.
-
Create a new file named
extract.pyin the root of your project directory and add the following code:This code loads the environment variables from the.envfile, initializes the Box SDK client, and prints the current user’s ID to validate that the client is working correctly. -
Run the script using the following command in your terminal:
If the Box SDK client is set up correctly, the console displays your user ID. For example:
6
Extract data
With a working Box SDK client, you can add the code to extract data from the document using Box AI.
-
Between the
get_box_clientfunction and themainfunction, add the following function:This function uses the Box AIcreate_ai_extract_structuredmethod to extract metadata from the specified file. Your BoxClient, the file ID, and the metadata template key created earlier are sent to the function, which returns the extracted metadata as a dictionary. -
Add the function call to extract the metadata in the
mainfunction. Ensure that the newmainfunction contains the following logic:The SDK handles the API call to Box AI and returns the extracted metadata as anAiExtractStructuredResponseobject. In this quick start, the code converts this object to a dictionary and returns theanswerfield that contains the extracted key/value pairs. -
Print out the extracted metadata to the console to verify that the extraction was successful by running the following command in your terminal:
If the extraction was successful, the console displays your user ID followed by the extracted metadata from the invoice document.
7
Add Box metadata to the file
Now that you have extracted metadata from the document, you can use these key/value pairs in your
application: push to databases, integrate with CRMs, feed to agents for processing, or trigger automated workflows.This quick start demonstrates pushing the extracted data back to Box as file metadata. Box metadata management
enables powerful filtering and search capabilities across your content. For example, you can query
all invoices over $500 from the last 30 days, create dashboards in Box Apps, or surface key document
insights directly in the Box web application.
-
Push the extracted metadata back to Box by adding the following function between the
extract_metadatafunction and themainfunction:This function uses thecreate_file_metadata_by_idmethod to attach metadata to the specified file, processing the BoxClient, file ID, metadata dictionary, and template key. The API itself returns aMetadataFullobject. The function converts this object to a dictionary and returns it. -
Add the function call to push the metadata in the
mainfunction. Ensure that the updatedmainfunction contains the following logic: -
Run the following command in your terminal:
If the script is successful, the console displays your user ID, the extracted metadata, and the attached metadata response from Box. For example:
