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
Upload a test file
After preparing the template, select a file to test. For this quick start, use this
sample stock purchase agreement.
- 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.
3
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.
4
Create the enhanced-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
enhanced-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:
5
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 and the file ID 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 stock purchase agreement.
