Skip to content

Instantly share code, notes, and snippets.

@gtalarico
Last active February 11, 2021 13:15
Show Gist options
  • Save gtalarico/e2114666e7fc38d5c144f7c514da687a to your computer and use it in GitHub Desktop.
Save gtalarico/e2114666e7fc38d5c144f7c514da687a to your computer and use it in GitHub Desktop.
Airtable Netlify Lambda Function Setup
// lambda/airtable.js
const Airtable = require('airtable')
Airtable.configure({
endpointUrl: 'https://api.airtable.com',
apiKey: process.env.AIRTABLE_KEY
})
const base = Airtable.base('appNtnZ99fkL1cByn')
exports.handler = function(event, context, callback) {
const allRecords = []
base('entries')
.select({
maxRecords: 100,
view: 'all'
})
.eachPage(
function page(records, fetchNextPage) {
records.forEach(function(record) {
allRecords.push(record)
})
fetchNextPage()
},
function done(err) {
if (err) {
callback(err)
} else {
const body = JSON.stringify({ records: allRecords })
const response = {
statusCode: 200,
body: body,
headers: {
'content-type': 'application/json',
'cache-control': 'Cache-Control: max-age=300, public'
}
}
callback(null, response)
}
}
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment