Back to all functions

Airtable: Search database

This function is able to search an item in an Airtable database and return the information in matching rows. This outputs the API response body directly.

Created By
Daniel D'Souza
Voiceflow
download-icon
INPUT VARIABLES
{
maxRecords
}
Max # of records to be returned
{
SearchValue
}
Customer ID or other search value
{
SearchColumn
}
The column you want to search through
{
AirtableToken
}
Your Airtable Personal API Token
{
baseId
}
ID for the base you are connecting too
{
tableIdOrName
}
tableID you are connecting to
share-icon
OUTPUT VARIABLES
{
Status
}
{
}
{
}
{
}
{
}
{
}
paths-icon
PATHS
{
}
{
}
{
}
{
}
{
}
{
}

Function Walkthrough

Function Code Snippet

 
  export default async function main(args) {
    let { customerID, AirtableToken, baseId, tableIdOrName, maxRecords, SearchValue, SearchColumn } = args.inputVars; //This is where you map the input variables you are importing
    SearchValue = SearchValue.toLowerCase()
    SearchValue = SearchValue.trim()
    const filterByFormula = `{${SearchColumn}} = '${SearchValue}'`


  //Check if the user has inputted the required variables
    if (!baseId || !tableIdOrName || !AirtableToken ) {
      return {
        //Returns the error path so we can continue the design
        next: { path: 'error' }, 
        //Renders a debug message in Voiceflow
        trace: [{ type: "debug", payload: { message: "Missing required input variables for this function" } }] 
      };
    }

  //Function to encode a url
  function encodeURIComponent(str) {
    // Manually create a mapping of characters to their percent-encoded forms
    const encodeMap = {
      '!': '%21',
      '#': '%23',
      '$': '%24',
      '&': '%26',
      '\'': '%27',
      '(': '%28',
      ')': '%29',
      '*': '%2A',
      '+': '%2B',
      ',': '%2C',
      '/': '%2F',
      ':': '%3A',
      ';': '%3B',
      '=': '%3D',
      '?': '%3F',
      '@': '%40',
      '[': '%5B',
      ']': '%5D',
      ' ': '%20',
      '\"': '%22',
      '-': '%2D',
      '.': '%2E',
      '_': '%5F',
      '~': '%7E',
      '%': '%25', // Must handle percent first to avoid double-encoding
      '{': '%7B', // Added encoding for {
      '}': '%7D', // Added encoding for }
    };

    let result = '';

    // Iterate through each character in the string
    for (let i = 0; i < str.length; i++) {
      const char = str[i];
      // If the character is in our map, use the encoded version, otherwise use the character itself
      result += encodeMap[char] || char;
    }

    return result;
  }

  // Function to encode the parameters into a query string
  function encodeQueryParams(params) {
    return Object.keys(params)
      .map(key => encodeURIComponent(key) + '=' + encodeURIComponent(params[key]))
      .join('&');
  }

  // Object to hold any query parameters
  const params = {
    maxRecords: maxRecords,
    view: "Grid view",
    filterByFormula: filterByFormula
    // Add more parameters here as needed
  };

  // Base URL without query parameters
  const baseUrl = `https://api.airtable.com/v0/${baseId}/${tableIdOrName}`;

  // Construct the full URL with encoded query parameters
  const url = `${baseUrl}?${encodeQueryParams(params)}`;

  // Setup the request options, including headers
  const config = {
    method: 'GET',
    headers: {
      'Authorization': `Bearer ${AirtableToken}`, // Replace with your actual token
    }
  };

  //This is where we made the fetch request, we use try-catch for error handling
    try {

      //Make the fetch request
      const response = await fetch(url, config); 

      // Check if the response status is OK (status in the range 200-299)
      if (!response.ok) {
      // If not OK, throw an error to be caught by the catch block
        throw new Error(`HTTP error! status: ${response.status}`);
      }

      //Map the fetch request response
      const responseBody = response.json; //IMPORTANT: functions uses .json instead of .json() - see documentation for details

      //Checks if the fetch request returned a body
      if (!responseBody || typeof responseBody !== 'object') {
      //If no body was returned, throw an error
        throw new Error(`Invalid or missing response body from the API`);
      }

      const CustomerStatus = responseBody.records[0].fields.Status

      // Create the return objects if this is successfull
      return {
        //Map our output variables
        outputVars: { responseBody: JSON.stringify(responseBody), Status: CustomerStatus },
        //Map the success path so we can continue in our flow
        next: { path: 'success' }
      };
    }

  //Catches all the errors we threw and displays the debug message
  catch (error) {
      return {
        //Maps the error path so we can continue in our design
        next: { path: 'error' },
        //Renders a debug message in Voiceflow with the error
        trace: [{ type: "debug", payload: { message: "Error:" + error.message + ' ' + url } }]
      };
    }
  }
copy-icon

Have something to share?

Share your creation with over 250,000 other global Voiceflow users.

ghraphic
No items found.