A #golang package to access the Airtable API.
go get github.com/Squirrel-Entreprise/airtable
Airtable uses simple token-based authentication. To generate or manage your API key, visit your account page.
Initialize client
a := airtable.New("xxx", "yyy")
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"])
}
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"])
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"])
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"])
table := airtable.Parameters{Name: "Products"}
if err := a.Delete(table, "recgnmCzr7u3jCB5w"); err != nil {
fmt.Println(err)
}