How to automate Supabase with Axiom.ai

Supabase (opens new window) is an open source Firebase alternative offering Postgres databases, Authentication, instant APIs, Edge Functions, Realtime subscriptions, Storage, and Vector embeddings. Combining Supabase with Axiom.ai can be a powerful way of levelling up your automations. From storing the data that you scrape from webpages, to retrieving data to be used in your automation, there is a lot you can achieve.

# Getting started


To get started, you will need to have the following steps completed:

  1. Sign up to Supabase.
  2. Create a database and a table, Supabase | Tables and Data (opens new window).
  3. Retrieve your project URL ("Project settings" → "API" → "Project URL").
  4. Retrieve your API key ("Project settings" → "API" → "Project API Keys").

Throughout this guide we will be using a "Customers" table, any code provided will need to be modified to your specific use case.

# Working with Supabase data in Axiom.ai


There are various actions that you can perform with the Supabase API and Axiom.ai. To learn more about the Supabase API, and see examples specific to your table, head into your Supabase dashboard, click "API docs" and find your table in the sidebar. Let's dive into some use cases.

# Write data to a Supabase table


To write data to your Supabase database, set up your Axiom.ai automation as normal. When you are ready to send your data, continue this guide.

To get started, add a Write Javascript step to your automation, we will then create variables to store key information used throughout the script. We will break down this script into sections, but this should all be combined into a single script in a single "Write Javascript" step.

// Change the [google-sheet-data] token to the token that stores your data and replace any item in <> with your data.
const ogData = [google-sheet-data];
const supabase_url = "<PROJECT_URL>/rest/v1/<TABLE_NAME>";
const api_key = "<API_KEY>";

Next, we will add a helper function that ensures that your data is formatted in a way that Supabase expects:

const format = (data) => {
  	const formatted_data = [];
	for (var i = 0; i < data.length; i++) {
        // Follow the format and add your own column names, incrementing the value in the second [] for each new piece of data.
    	formatted_data.push({"first_name": data[i][0], "last_name": data[i][1], "email": data[i][2]});
    }
  
  return formatted_data;
}

Finally, we want to send the data to Supabase. There isn't anything that needs to be changed here, but you may want to use the result in later steps of your automation, you'll need to return this value.

const sendToSupabase = async () => {
 	try {
    	const response = await fetch(supabase_url, {
        	method: 'POST',
          	headers: {
            	'apikey': api_key,
              'Authorization': `Bearer ${api_key}`,
              'Content-Type': 'application/json'
            },
          	body: JSON.stringify(format(ogData))
        });
                               
        if (!response.ok) {
        	console.error("Something went wrong with response", response); 
            return;
        }
      
        const result = await response.json();
        console.log(result);
    } catch (error) {
    	console.error("Something went wrong", error);
    }
}

To use this as is, call the sendToSupabase function at the end of your script.

sendToSupabase();

If you are using a large amount of data and you are struggling to pass it all in a single call, you may require the use of batching - breaking down the data and sending it as smaller chunks. This can also help work around the possibility of Supabase not writing rows due to an issue with a single row of data. We will need to use a slightly modified sendToSupabase function:

// Change this to meet your requirements.
const batchSize = 10;

const sendToSupabase = async () => {
    for (var i = 0; i < ogData.length; i += batchSize) {
        try {
    	    const response = await fetch(supabase_url, {
        	    method: 'POST',
          	    headers: {
            	    'apikey': api_key,
              	    'Authorization': `Bearer ${api_key}`,
              	    'Content-Type': 'application/json'
                },
          	    body: JSON.stringify(format(ogData.slice(i, i + batchSize)))
            });
                               
            if (!response.ok) {
        	    console.error("Something went wrong with response", response); 
                return;
            }
      
            const result = await response.json();
            console.log(result);
        } catch (e) {
    	    console.log("Something went wrong", e);
        }
    }
}

To use this, call the sendToSupabase function at the end of your script.

sendToSupabase();

# Read data from a Supabase table


Reading data from a Supabase table allows you to make use of this data within your Axiom.ai automations. To get started, add a Write Javascript step to your automation.

We will first create variables to store key information used throughout the script. We will break down this script into sections, but this should all be combined into a single script in a single "Write Javascript" step.

// Replace any item in <> with your data.
const supabase_url = "<PROJECT_URL>/rest/v1/<TABLE_NAME>";
const api_key = "<API_KEY>";

Your supabase_url variable will need to be slightly modified, depending on what data you are looking to return, for example:

  • Return all columns: "<PROJECT_URL>/rest/v1/<TABLE_NAME>?select=*"
  • Return specific columns: "<PROJECT_URL>/rest/v1/<TABLE_NAME>?select=some_column,other_column"
  • Filtered: "<PROJECT_URL>/rest/v1/<TABLE_NAME>?id=eq.1", learn more (opens new window).

Next, we will want to retrieve the data from your Supabase database. You won't need to make any changes to this code:

const retrieveData = async () => {
	try {
      const response = await fetch(supabase_url, {
        headers: {
          'apikey': api_key,
          'Authorization': `Bearer ${api_key}`
        }
      });
        
      if (!response.ok) {
        console.error("Something went wrong");
        return;
      }
      
      const result = await response.json();
      return result;
    } catch(e) {
      console.error("Something went wrong", e);
    }
}

const data = await retrieveData();

The data variable should now contain the data from your database. We'll need to change the format of this data and return it to allow your automation to use it, we'll use the following code, again, no changes are required:

const format = () => {
  const formatted_data = [];
  for (var i = 0; i < data.length; i++) {
  	formatted_data.push(Object.values(data[i]))
  }
  
  return formatted_data;
}

return format();

Your data will now be stored in the code-data data token that is output from the "Write Javascript" step. Learn more about How to pass data between steps in your automation. Use the Loop through data step to loop through the rows of data returned.

# Triggering an Axiom.ai automation from a Supabase database event


Supabase offers the ability to trigger webhooks based on events happening within your database. Triggering Axiom.ai automations with webhooks requires a paid plan, see pricing for more details.

To get started, add a Receive data from another app step to your automation - we recommend adding this as the first step of your automation. For more information on setting test data, see Receive data from another app.

In Supabase, head to your Dashboard, then into "Integrations" and "Database Webhooks" - it should already be installed. Click "Create a new hook" to create a new webhook. Configure as instructed by the user interface, specifically setting the following fields with Axiom.ai data:

  1. HTTP Request - Method: Set to 'POST'.
  2. HTTP Request - URL: the Axiom.ai endpoint is https://lar.axiom.ai/api/v3/trigger.
  3. Headers: often pre-configured, 'Content-Type': 'application/json'.

Note, it's not currently possible to send data to Axiom.ai with these webhooks - however, an automation that retrieves data from Supabase can be triggered using this method. Supabase webhooks are for notification purpose only.

# Testing your workflow


To test adding data to Supabase, click "Run" within the Builder - open up your Supabase dashboard and open the table to view any changes. To test retrieving data from Supabase, add an output for your data, for example, a Write data to a Google Sheet step.

To test triggering an Axiom.ai automation from an event, perform the event that you have set up within your automation - for example, by inserting or updating data within your database.

# Wrapping up


Combining Supabase and Axiom.ai allows you more flexibility on your data storage to power up your automations. Using Supabase opens up the possibility to connect external databases using their integrations such as S3, Stripe, Firebase and much more. We are excited to see what you build!


Information accurate as of November 2024.