Help

Solved: Adding a single select field (singleSelect) when creating a new table in Airtable

217 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Matthew_Schneid
4 - Data Explorer
4 - Data Explorer

I spent a couple of hours experimenting with Google Colab and the Airtable API to figure out how to create a new table that includes a singleSelect field.

I'm sharing the example I needed but couldn't find.

This example illustrates how to structure the JSON to define a new table with the following field types:

  • a number field (with precision)
  • a singleSelect field (with and without predefined options),
  • a singleLineText field

Here is the example I needed:

 

# Define the data payload for creating the new table
# This example demonstrates how to structure the JSON to create a new table in Airtable,
# including different field types such as "number", "singleSelect", and "singleLineText".

data = {
    "description": "Table description",
    "fields": [
        {
            "type": "number",
            "options": {
                "precision": 0
            },
            "name": "Number Field Name"
        },
        {
            "type": "singleSelect",
            "options": {
                "choices": []
            },
            "name": "Single Select Text Field Name"
        },
        {
            "type": "singleSelect",
            "options": {
                "choices": [
                    {"name": "Option 1"},
                    {"name": "Option 2"},
                    {"name": "Option 3"}
                ]
            },
            "name": "Single Select Text Field Name with options"
        },
        {
            "type": "singleLineText",
            "name": "Single Line Text Field Name"
        }
    ],
    "name": "Table Name"
}

Notes:

  • Even if you don't know what choices you need for your singleSelect menu, you still need to pass "choices": [] in options.
import requests
import json

# Define the URL for creating a new table
base_id = "your_base_id"
AIRTABLE_API_KEY = "your_api_key"

url = f"https://api.airtable.com/v0/meta/bases/{base_id}/tables"

# Define the headers for authentication and content type
headers = {
    "Authorization": f"Bearer {AIRTABLE_API_KEY}",
    "Content-Type": "application/json"
}

# Send the POST request to create the new table
response = requests.post(url, headers=headers, json=data)

# Check if the request was successful
if response.status_code == 200:
    print("Table created successfully!")
    print(response.json())
else:
    print("Error:", response.json())

 I hope this is helpful!

0 Replies 0