Skip to main content
Box AI exposes intelligent extraction capabilities that enable developers to automatically extract key-value pairs from documents through a single API call. This powerful feature transforms unstructured document content into actionable metadata without manual data entry, streamlining document processing workflows for invoices, forms, contracts, and other business documents. This quick start demonstrates how to configure the Box Python SDK and use Box AI to extract data from a W-2 stored in Box.
1

Create and configure a Box application

The first step for any Box Platform integration is to create and configure a Box application.
  1. Go to Box Developer Console.
  2. For this quick start, create an App with the Client Credentials Grant application type.
  3. Once the app is created, enable the following scopes:
    • Read all files and folders stored in Box
    • Write all files and folders stored in Box
    • Manage AI
For more information about creating a new Box application, see Create your first application.
2

Upload a test file

After preparing the template, select a file to test. For this quick start, use this sample W-2.
  1. Download the test document, and then drag and drop it into your Box account.
  2. 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/2064123286902 In this case, the file ID is 2064123286902.
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.
  1. Create a new directory for your project and navigate into it.
  2. Create a virtual environment:
    python3 -m venv .venv
    source .venv/bin/activate
    
  3. Install the Box Python SDK:
    pip install "boxsdk~=10"
    
  4. Install the python-dotenv package to load environment variables from the .env file:
    pip install python-dotenv
    
  5. Create an .env file 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:
     BOX_DEVELOPER_TOKEN=your_box_developer_token
    
     BOX_METADATA_TEMPLATE_KEY=your_metadata_template_key
     BOX_FILE_ID=your_box_file_id
    
To get your developer token, go to the Box Developer Console, open your app, and navigate to the Configuration tab.
  1. Click Generate Developer Token to create a new token.
For simplicity, this quick start uses a short-lived developer token. In production, you should authenticate using your app’s configured method (for example, Client Credentials Grant) instead of a developer token.
4

Create the freeform-extract.py file

Your development environment is now ready to create the Python script to extract data from the document using Box AI.
  1. Create a new file named freeform-extract.py in the root of your project directory and add the following code:
    import os
    
    from dotenv import load_dotenv
    
    from box_sdk_gen import (
        AiItemBase,
        BoxClient,
        BoxDeveloperTokenAuth
    )
    
    # Set up environment variables
    load_dotenv()
    
    developer_token = os.getenv("BOX_DEVELOPER_TOKEN")
    file_id = os.getenv("BOX_FILE_ID")
    
    def get_box_client(token: str) -> BoxClient:
        
        if not developer_token:
            raise ValueError("BOX_DEVELOPER_TOKEN is not set in environment variables.")
        
        auth = BoxDeveloperTokenAuth(token=token)
        client = BoxClient(auth=auth)
    
        return client
    
    def main():
        client = get_box_client(token=developer_token)
    
        # Get the current user and print the ID to validate the client is working
        me = client.users.get_user_me()
        print(f"My user ID is {me.id}")
    
    if __name__ == "__main__":
        main()
    
    This code loads the environment variables from the .env file, initializes the Box SDK client, and prints the current user’s ID to validate that the client is working correctly.
  2. Run the script using the following command in your terminal:
    python freeform-extract.py
    
    If the Box SDK client is set up correctly, the console displays your user ID. For example:
    My user ID is 123456789
    
5

Extract data

With a working Box SDK client, you can add the code to extract data from the document using Box AI.
  1. Between the get_box_client function and the main function, add the following function:
    def extract_metadata(client: BoxClient, file_id: str) -> str:
    
        prompt = """
        firstName, lastName, wages, federalTaxWithheld, 
        socialSecurityWages, socialSecurityTaxWithheld, 
        medicareWagesAndTips, medicareTaxWithheld, 
        stateWages, stateTaxWithheld, 
        localWagesAndTips, localTaxWithheld
        """
    
        metadata = client.ai.create_ai_extract(
            prompt,
            [AiItemBase(id=file_id)]
        )
        
        return metadata.to_dict()['answer']
    
    This function uses the Box AI create_ai_extract method 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.
  2. Add the function call to extract the metadata in the main function. Ensure that the new main function contains the following logic:
    def main():
        client = get_box_client(token=developer_token)
    
        me = client.users.get_user_me()
        print(f"My user ID is {me.id}")
    
        metadata = extract_metadata(client=client, file_id=file_id)
    
        print(f"\n\nExtracted Metadata: {metadata}")
    
    The SDK handles the API call to Box AI and returns the extracted metadata as an AiExtractResponse object. In this quick start, the code converts this object to a dictionary and returns the answer field that contains the extracted key/value pairs.
  3. Print out the extracted metadata to the console to verify that the extraction was successful by running the following command in your terminal:
    python freeform-extract.py
    
    If the extraction was successful, the console displays your user ID followed by the extracted metadata from the W-2.
    My user ID is 123456789
    
    Extracted Metadata: {"firstName": "Wayne", "lastName": "Gatsby", "wages": "355000", "federalTaxWithheld": "125600", "socialSecurityWages": "355000", "socialSecurityTaxWithheld": "12300", "medicareWagesAndTips": "355000", "medicareTaxWithheld": "13200", "stateWages": "355,000", "stateTaxWithheld": "15000", "localWagesAndTips": "355000", "localTaxWithheld": "8000"}
    

Resources