Skip to content

Squirrel-Entreprise/airtable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Golang Airtable API

GoDoc Go Go Report Card codecov

A #golang package to access the Airtable API.

Table of contents

Installation

    go get github.com/Squirrel-Entreprise/airtable

Aitable API

Airtable uses simple token-based authentication. To generate or manage your API key, visit your account page.

Getting started

Initialize client

a := airtable.New("xxx", "yyy")

List table records

productsParameters := airtable.Parameters{
	Name:       "Products", // Name of the table
	MaxRecords: "100", // Max records to return
    	PageSize:   "10",
	View:       "Grid view", // View name
	FilterByFormula: fmt.Sprintf(`Name="%s"`, "Apple"), // Filter by formula
	Fields: []string{ // Fields to return
		"Name",
		"Category",
	},
	Sort: []airtable.Sort{
		{
			Field:     "Category",
			Direction: airtable.Descending,
		},
	},
}

var products airtable.AirtableList

if err := a.List(productsParameters, &products); err != nil {
	fmt.Println(err)
}

for _, p := range products.Records {
	fmt.Println(p.ID, p.Fields["Name"], p.Fields["Category"])
}

Get table record

product := airtable.AirtableItem{}
table := airtable.Parameters{Name: "Products"}
if err := a.Get(table, "recj2fwn8nSQhR9Gg", &product); err != nil {
	fmt.Println(err)
}

fmt.Println(product.ID, product.Fields["Name"], product.Fields["Category"])

Create table record

type porductPayload struct {
	Fields struct {
		Name     string  `json:"Name"`
		Category string  `json:"Category"`
		Price    float64 `json:"Price"`
	} `json:"fields"`
}

newProduct := porductPayload{}
newProduct.Fields.Name = "Framboise"
newProduct.Fields.Category = "Fruit"
newProduct.Fields.Price = 10.0

payload, err := json.Marshal(newProduct)
if err != nil {
	fmt.Println(err)
}

product := airtable.AirtableItem{}

table := airtable.Parameters{Name: "Products"}
if err := a.Create(table, payload, &product); err != nil {
	fmt.Println(err)
}

fmt.Println(product.ID, product.Fields["Name"], product.Fields["Price"])

Update table record

type porductPayload struct {
	Fields struct {
		Price float64 `json:"Price"`
	} `json:"fields"`
}

updateProduct := porductPayload{}
updateProduct.Fields.Price = 11.0

payload, err := json.Marshal(updateProduct)
if err != nil {
	fmt.Println(err)
}

product := airtable.AirtableItem{}

table := airtable.Parameters{Name: "Products"}
if err := a.Update(table, "recgnmCzr7u3jCB5w", payload, &product); err != nil {
	fmt.Println(err)
}

fmt.Println(product.ID, product.Fields["Name"], product.Fields["Price"])

Delete table record

table := airtable.Parameters{Name: "Products"}
if err := a.Delete(table, "recgnmCzr7u3jCB5w"); err != nil {
	fmt.Println(err)
}