Help

The Community will be undergoing maintenance from Friday February 21 - Friday, February 29 and will be "read only" during this time. To learn more, check out our Announcements blog post.

Airtable API using Node.js create field examples?

Solved
Jump to Solution
1791 1
cancel
Showing results for 
Search instead for 
Did you mean: 
Devmatiq
4 - Data Explorer
4 - Data Explorer

Does anyone know of documentation that has an example of creating each field type using Node.js?

There are certain field types I am running into issues with such as date fields and currency fields and I can not find much documentation of when an example for each field completed looks like.

1 Solution

Accepted Solutions
Devmatiq
4 - Data Explorer
4 - Data Explorer

Well, looks like I figured it out.

After a while playing around a bit I was able to figure out what fields were supported and what fields are not supported.

Here is a script that have every supported field, and all of their options to create a table and to create the fields.

 

 

const axios = require('axios');

const apiKey = 'API_KEY';
const baseId = 'BASE_ID';
const apiUrl = `https://api.airtable.com/v0/meta/bases/${baseId}`;
const linkedTableId = 'TABLE_ID_TO_LINK_TO';

const headers = {
Authorization: `Bearer ${apiKey}`,
'Content-Type': 'application/json'
};

async function createTable(tableName, fields) {
try {
const response = await axios.post(`${apiUrl}/tables`, {
fields: fields,
name: tableName
}, { headers });

console.log(`Created table ${tableName}:`, response.data);
} catch (error) {
console.error(`Error creating table ${tableName}:`, error.response ? error.response.data : error.message);
}
}

const tableName = "Script Template";
const fields = [
{ name: "Single Line Text", type: "singleLineText" },
{ name: "Attachment", type: "multipleAttachments" },
{ name: "Barcode", type: "barcode" },
{ name: "Checkbox", type: "checkbox", options: { icon: "check", color: "greenBright" } },
{ name: "Collaborator", type: "singleCollaborator" },
{ name: "Currency", type: "currency", options: { precision: 2, symbol: "$" } },
{ name: "Date", type: "date", options: { dateFormat: { name: "iso", format: "YYYY-MM-DD" } } },
{ name: "Date and Time", type: "dateTime", options: { dateFormat: { name: "iso", format: "YYYY-MM-DD" }, timeFormat: { name: "24hour", format: "HH:mm" }, timeZone: "Europe/London" } },
{ name: "Duration", type: "duration", options: { durationFormat: "h:mm:ss" } },
{ name: "Email", type: "email" },
{ name: "Link to Another Record", type: "multipleRecordLinks", options: { linkedTableId: linkedTableId } },
{ name: "Long Text", type: "multilineText" },
{ name: "Multiple Collaborators", type: "multipleCollaborators" },
{ name: "Multiple Selects", type: "multipleSelects", options: { choices: [{ name: "Option 1" }, { name: "Option 2" }] } },
{ name: "Number", type: "number", options: { precision: 2 } },
{ name: "Percent", type: "percent", options: { precision: 2 } },
{ name: "Phone", type: "phoneNumber" },
{ name: "Rating", type: "rating", options: { icon: "star", color: "yellowBright", max: 5 } },
{ name: "Rich Text", type: "richText" },
{ name: "Single Select", type: "singleSelect", options: { choices: [{ name: "Option 1" }, { name: "Option 2" }] } },
{ name: "URL", type: "url" }
];

createTable(tableName, fields);

 



This script can be utilized as a template.

To use it simply place your api key, base id, and table id to any table you would like linked if you are creating a linked records field.

See Solution in Thread

1 Reply 1
Devmatiq
4 - Data Explorer
4 - Data Explorer

Well, looks like I figured it out.

After a while playing around a bit I was able to figure out what fields were supported and what fields are not supported.

Here is a script that have every supported field, and all of their options to create a table and to create the fields.

 

 

const axios = require('axios');

const apiKey = 'API_KEY';
const baseId = 'BASE_ID';
const apiUrl = `https://api.airtable.com/v0/meta/bases/${baseId}`;
const linkedTableId = 'TABLE_ID_TO_LINK_TO';

const headers = {
Authorization: `Bearer ${apiKey}`,
'Content-Type': 'application/json'
};

async function createTable(tableName, fields) {
try {
const response = await axios.post(`${apiUrl}/tables`, {
fields: fields,
name: tableName
}, { headers });

console.log(`Created table ${tableName}:`, response.data);
} catch (error) {
console.error(`Error creating table ${tableName}:`, error.response ? error.response.data : error.message);
}
}

const tableName = "Script Template";
const fields = [
{ name: "Single Line Text", type: "singleLineText" },
{ name: "Attachment", type: "multipleAttachments" },
{ name: "Barcode", type: "barcode" },
{ name: "Checkbox", type: "checkbox", options: { icon: "check", color: "greenBright" } },
{ name: "Collaborator", type: "singleCollaborator" },
{ name: "Currency", type: "currency", options: { precision: 2, symbol: "$" } },
{ name: "Date", type: "date", options: { dateFormat: { name: "iso", format: "YYYY-MM-DD" } } },
{ name: "Date and Time", type: "dateTime", options: { dateFormat: { name: "iso", format: "YYYY-MM-DD" }, timeFormat: { name: "24hour", format: "HH:mm" }, timeZone: "Europe/London" } },
{ name: "Duration", type: "duration", options: { durationFormat: "h:mm:ss" } },
{ name: "Email", type: "email" },
{ name: "Link to Another Record", type: "multipleRecordLinks", options: { linkedTableId: linkedTableId } },
{ name: "Long Text", type: "multilineText" },
{ name: "Multiple Collaborators", type: "multipleCollaborators" },
{ name: "Multiple Selects", type: "multipleSelects", options: { choices: [{ name: "Option 1" }, { name: "Option 2" }] } },
{ name: "Number", type: "number", options: { precision: 2 } },
{ name: "Percent", type: "percent", options: { precision: 2 } },
{ name: "Phone", type: "phoneNumber" },
{ name: "Rating", type: "rating", options: { icon: "star", color: "yellowBright", max: 5 } },
{ name: "Rich Text", type: "richText" },
{ name: "Single Select", type: "singleSelect", options: { choices: [{ name: "Option 1" }, { name: "Option 2" }] } },
{ name: "URL", type: "url" }
];

createTable(tableName, fields);

 



This script can be utilized as a template.

To use it simply place your api key, base id, and table id to any table you would like linked if you are creating a linked records field.