0

Get Record

by
Published Jun 6, 2022
Script airtable Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 10 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
  recordId?: string,
15
) {
16
  const baseUrl = `https://api.airtable.com/v0/${atTable.baseId}/${encodeURIComponent(
17
    atTable.tableName,
18
  )}`;
19

20
  if (recordId) {
21
    const response = await fetch(`${baseUrl}/${recordId}`, {
22
      method: "GET",
23
      headers: {
24
        Authorization: `Bearer ${atCon.apiKey}`,
25
      },
26
    });
27
    if (!response.ok) {
28
      throw new Error(`${response.status} ${await response.text()}`);
29
    }
30
    const record = await response.json();
31
    return { result: record };
32
  } else {
33
    const response = await fetch(baseUrl, {
34
      method: "GET",
35
      headers: {
36
        Authorization: `Bearer ${atCon.apiKey}`,
37
      },
38
    });
39
    if (!response.ok) {
40
      throw new Error(`${response.status} ${await response.text()}`);
41
    }
42
    const records = await response.json();
43
    return { result: records };
44
  }
45
}
46

Other submissions
  • Submitted by rossmccrann Deno
    Created 1433 days ago
    1
    import { Airtable } from "https://deno.land/x/airtable/mod.ts";
    2
    
    
    3
    
    
    4
    export async function main(at_con: wmill.Resource<"airtable">, recordId: string) {
    5
        const airtable = new Airtable(at_con);
    6
    
    
    7
        const record = await airtable.find(recordId)
    8
      
    9
        return {result: record};
    10
    }
  • Submitted by henri186 Deno
    Created 402 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
      recordId?: string,
    15
    ) {
    16
      const airtable = new Airtable({ ...atCon, ...atTable });
    17
    
    
    18
      if (recordId) {
    19
        const record = await airtable.find(recordId);
    20
        return { result: record };
    21
      } else {
    22
        const records = await airtable.select().all();
    23
        return { result: records };
    24
      }
    25
    }
    26