Adding Data to a Database = like airtable etc

Is it possible to update a database from a pickaxe ?

If Airtable has an API to access their tables from, then yes it is possible! You can configure it as a custom action or we could look into adding it as a pre-built action.

But that’s assuming Airtable allows you to do this. I don’t know this off the top of my head!

1 Like

@ihmunro hey mate, yes Airtable has APIs. In Airtable you need to know the record ID before you can update any field within that record ID.

So you need two API calls, one to extract the record ID’s (to identify the right one) and then a second call to update it.

I haven’t spent time thinking on how to create an Airtable custom action so at this stage I send thee data to Make and then Airtable.

1 Like

Hi Mike

Yes, Airtable has an API

1 Like

Many thanks

Would love to see how this would be achieved

@ihmunro your every wish will be granted :rofl:

import os
import requests

def airtable_create_record(name: str, email: str, country: str):
    """
    Create a new record in Airtable with the data from the chat

    Args:
        name (string): User's email
        email (string): User's email
        country (string): User's country
    Envs:
        AIRTABLE_API_KEY (string): Airtable API key
        AIRTABLE_BASE_ID (string): Airtable Base ID
        AIRTABLE_TABLE_ID (string): Airtable Table ID
    """

    # Insert your PYTHON code below. You can access environment variables using os.environ[].
    # Currently, only the requests library is supported, but more libraries will be available soon.
    # Use print statements or return values to display results to the user.
    # If you save a png, pdf, csv, jpg, webp, gif, or html file in the root directory, it will be automatically displayed to the user.
    # You do not have to call this function as the bot will automatically call and fill in the parameters.

    # Airtable API configuration
    airtable_base_id = os.environ["AIRTABLE_BASE_ID"]  # Add your Airtable Base ID to the environment variable
    airtable_table_id = os.environ["AIRTABLE_TABLE_ID"]  # Add your Table ID or Table Name to the environment variable
    airtable_api_key = os.environ["AIRTABLE_API_KEY"]  # Add your Airtable API Key to the environment variable

    # Check if all necessary variables are set
    if not airtable_base_id or not airtable_table_id or not airtable_api_key:
        print("Error: Missing Airtable configuration in environment variables.")
        return None

    # API endpoint for creating records
    url = f"https://api.airtable.com/v0/{airtable_base_id}/{airtable_table_id}"

    # Headers including authorization
    headers = {
        "Authorization": f"Bearer {airtable_api_key}",
        "Content-Type": "application/json"
    }

    # Payload with record data
    payload = {
        "fields": {
            "Name": name,
            "Email": email,
            "Country": country
        }
    }

    # Send the POST request
    try:
        response = requests.post(url, headers=headers, json=payload, timeout=10)

        # Check if the request was successful
        if response.status_code == 200 or response.status_code == 201:
            print("Record created successfully!")
            print(response.json())  # Print response for verification
        else:
            print(f"Failed to create record. Status code: {response.status_code}, Response: {response.text}")

    except requests.exceptions.Timeout:
        print("The request timed out.")
    except requests.exceptions.RequestException as e:
        print(f"An error occurred: {e}")

# Example usage
# airtable_create_record("John Doe", "johndoe@example.com", "Australia")
2 Likes

Many thanks AB

Really appreciate it.

That worked.

I did try to add in a status field - like ToDo, but for some reason it did not like that, so removed it and it worked.

Many thanks