May 26, 2024 05:18 PM
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.
Solved! Go to Solution.
May 26, 2024 06:40 PM
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);
May 26, 2024 06:40 PM
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);