Create single record

Script airtable Verified

by nicolasxxmctoastxx ยท 6/6/2022

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 4 days ago
1
//native
2

3
type Airtable = {
4
  apiKey: string;
5
};
6

7
type AirtableTable = {
8
  baseId: string;
9
  tableName: string;
10
};
11
export async function main(
12
  atCon: Airtable,
13
  atTable: AirtableTable,
14
  newRecord: object,
15
) {
16
  const url = `https://api.airtable.com/v0/${atTable.baseId}/${encodeURIComponent(
17
    atTable.tableName,
18
  )}`;
19

20
  const response = await fetch(url, {
21
    method: "POST",
22
    headers: {
23
      Authorization: `Bearer ${atCon.apiKey}`,
24
      "Content-Type": "application/json",
25
    },
26
    body: JSON.stringify({ fields: newRecord }),
27
  });
28

29
  if (!response.ok) {
30
    throw new Error(`${response.status} ${await response.text()}`);
31
  }
32

33
  const createOne = await response.json();
34

35
  return createOne;
36
}
37

Other submissions
  • Submitted by rossmccrann Deno
    Created 1029 days ago
    1
    import { Airtable } from "https://deno.land/x/airtable/mod.ts";
    2
    
    
    3
    type Airtable = {
    4
      apiKey: string;
    5
    };
    6
    
    
    7
    type AirtableTable = {
    8
      baseId: string;
    9
      tableName: string;
    10
    };
    11
    export async function main(
    12
      at_con: Airtable,
    13
      at_table: AirtableTable,
    14
      new_record: object,
    15
    ) {
    16
      const airtable = new Airtable({ ...at_con, ...at_table });
    17
    
    
    18
      const createOne = await airtable.create(new_record);
    19
    
    
    20
      return { message: "Created record in table" };
    21
    }
    22
    
    
  • Submitted by hugo697 Deno
    Created 396 days ago
    1
    import { Airtable } from "https://deno.land/x/airtable/mod.ts";
    2
    
    
    3
    type Airtable = {
    4
      apiKey: string;
    5
    };
    6
    
    
    7
    type AirtableTable = {
    8
      baseId: string;
    9
      tableName: string;
    10
    };
    11
    export async function main(
    12
      atCon: Airtable,
    13
      atTable: AirtableTable,
    14
      newRecord: object,
    15
    ) {
    16
      const airtable = new Airtable({ ...atCon, ...atTable });
    17
    
    
    18
      const createOne = await airtable.create(newRecord);
    19
    
    
    20
      return createOne;
    21
    }