Hey there, fellow developer! Ready to supercharge your forms with the Typeform API? Let's dive into building a slick integration using the @typeform/api-client
package. This powerhouse lets you fetch form data, handle responses, and even create forms on the fly. Buckle up!
Before we jump in, make sure you've got:
Let's get this show on the road:
mkdir typeform-integration && cd typeform-integration npm init -y npm install @typeform/api-client
Time to make friends with the API:
const { createClient } = require('@typeform/api-client') const typeformAPI = createClient({ token: 'YOUR_API_KEY_HERE' })
Let's grab some forms:
async function getForms() { try { const forms = await typeformAPI.forms.list() console.log(forms) } catch (error) { console.error('Oops!', error) } } getForms()
Want details on a specific form? No sweat:
async function getFormDetails(formId) { try { const form = await typeformAPI.forms.get({ uid: formId }) console.log(form) } catch (error) { console.error('Uh-oh!', error) } } getFormDetails('YOUR_FORM_ID')
Let's fetch those valuable responses:
async function getResponses(formId) { try { const responses = await typeformAPI.responses.list({ uid: formId }) console.log(responses) } catch (error) { console.error('Yikes!', error) } } getResponses('YOUR_FORM_ID')
Got a ton of responses? Pagination's got your back:
async function getAllResponses(formId) { let allResponses = [] let pageToken = null do { const { items, page_count } = await typeformAPI.responses.list({ uid: formId, page_size: 1000, after: pageToken }) allResponses = allResponses.concat(items) pageToken = page_count > 0 ? items[items.length - 1].token : null } while (pageToken) console.log(allResponses) } getAllResponses('YOUR_FORM_ID')
Real-time data? You got it. Set up a webhook:
async function createWebhook(formId, url) { try { const webhook = await typeformAPI.webhooks.create({ uid: formId, tag: 'my-cool-webhook', url: url, enabled: true }) console.log('Webhook created:', webhook) } catch (error) { console.error('Webhook creation failed:', error) } } createWebhook('YOUR_FORM_ID', 'https://your-server.com/webhook')
Now, handle that incoming data like a boss:
const express = require('express') const app = express() app.use(express.json()) app.post('/webhook', (req, res) => { const formResponse = req.body console.log('New response:', formResponse) res.sendStatus(200) }) app.listen(3000, () => console.log('Webhook server running on port 3000'))
Always wrap your API calls in try-catch blocks (like we've been doing). And hey, respect those rate limits – Typeform's got 'em for a reason!
Feeling adventurous? Let's update a form field:
async function updateFormField(formId, fieldId, newTitle) { try { await typeformAPI.forms.update({ uid: formId, data: { fields: [{ id: fieldId, title: newTitle }] } }) console.log('Field updated successfully!') } catch (error) { console.error('Update failed:', error) } } updateFormField('YOUR_FORM_ID', 'FIELD_ID', 'New awesome question')
And there you have it! You're now armed and dangerous with Typeform API integration skills. Remember, this is just scratching the surface – there's a whole world of form-building possibilities out there. Check out the Typeform API docs for more cool tricks.
Now go forth and create some killer forms! 🚀