Gateway over AirTable APIs
Let's put a gateway over the AirTable Event Planning example in just a 5 quick steps. We'll start by showing you how you can create a new API which inserts data into the Attendees table.
1: Environment Variables
Let's setup our environment variables. Open environment.json and delete any example variables. Add two new variables - one config and one secret:
ATTENDEES_TABLE_URL
(config)API_KEY
(secret)
2: Setup Airtable
Create a new AirTable workspace from the
Event Planning Template.
When done, navigate to https://airtable.com/api and
select your new workspace. On the side-menu select, Attendees Table >
Create Records. You will see an example request like below, copy the URL
from that example and paste it in to your ATTENDEES_TABLE_URL
environment
config.
Get your
AirTable API Key
and paste it into you API_KEY
environment secret.
Be sure to press Save (you can also use CTRL+S or ⌘+S).
3: Attendees Module
Create a new Empty Module and name it attendees.ts
. This will be where we
write some custom code to call AirTable
Copy the following code into that file.
import { ZuploContext, ZuploRequest } from "@zuplo/runtime";
import env from "@app/environment";
export default async function (request: ZuploRequest, context: ZuploContext) {
const body = await request.json();
const data = {
records: [
{
fields: {
Name: body.name,
Email: body.email,
},
},
],
};
const response = await fetch(env.ATTENDEES_TABLE_URL, {
method: "POST",
headers: {
Authorization: `Bearer ${env.API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify(data),
});
if (!response.ok) {
return new Response("Error calling AirTable!", {
status: 500,
});
}
return new Response("Success", {
status: 200,
});
}
4: Routes Config
Open the routes.json file and change the path of the existing route to
be /attendees
, set the method to POST
and save the file.
Use the function picker [...] and use it to select the attendees
module and
default
export as the request handler.
5: Test
Open the Route Tester. Set the path to /v1/attendees
and the method to POST
and set the body as follows
{
"name" : "Your Name",
"email": "[email protected]"
}
Congratulations
Your API is now live! BTW - you can see the URL in settings. Next, try posting to your API from curl.
Also, why not try creating a schema and using a policy to validate the incoming body?
Why not try one of the other getting started guides (above) or some of the examples in our documentation: