Airtable: Search database

Template
Function
1
Template
Function
by
Daniel D'Souza

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:

Heading

Voiceflow APIs used:

Channels
No items found.
Created By
Daniel D'Souza
This is some text inside of a div block.
Overview
This is some text inside of a div block.
by
This is some text inside of a div block.

Heading 1

Heading 2

Heading 3

Heading 4

Heading 5
Heading 6

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

Block quote

Ordered list

  1. Item 1
  2. Item 2
  3. Item 3

Unordered list

  • Item A
  • Item B
  • Item C

Text link

Bold text

Emphasis

Superscript

Subscript

Heading
Based in
This is some text inside of a div block.
Heading

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

Explore More Templates

Build and submit a Template to have it featured in the community.

ghraphic
No items found.
No items found.