Hey there, fellow Ruby enthusiast! Ready to supercharge your forms with Typeform's API? You're in for a treat. We'll be using the nifty typeform
gem to make our lives easier. Let's dive in and create some magic!
Before we get our hands dirty, make sure you've got:
typeform
gem installed (gem install typeform
)Got all that? Great! Let's roll.
First things first, let's get that client set up:
require 'typeform' client = Typeform::Client.new(api_key: 'YOUR_API_KEY_HERE')
Easy peasy, right? Now we're ready to rock and roll with Typeform's API.
Want to see what forms you've got? No problem:
forms = client.forms.list forms.each { |form| puts form.title }
Need the nitty-gritty on a specific form? We've got you covered:
form = client.forms.get(form_id: 'YOUR_FORM_ID') puts form.title puts form.fields.map(&:title)
Let's grab those valuable responses:
responses = client.responses.list(form_id: 'YOUR_FORM_ID') responses.items.each do |response| puts response.submitted_at puts response.answers end
Need to filter or paginate? Just add some params:
responses = client.responses.list( form_id: 'YOUR_FORM_ID', page_size: 20, since: '2023-01-01T00:00:00Z' )
Feeling creative? Let's build a form from scratch:
new_form = client.forms.create( title: 'My Awesome Form', fields: [ { type: 'short_text', title: 'What's your name?' }, { type: 'email', title: 'Your email, please!' } ] ) puts "Created form with ID: #{new_form.id}"
Need to tweak an existing form? No sweat:
client.forms.update( form_id: 'EXISTING_FORM_ID', title: 'My Even More Awesome Form', fields: [{ type: 'multiple_choice', title: 'Pick your favorite color', choices: ['Red', 'Blue', 'Green'] }] )
Want real-time updates? Webhooks to the rescue:
webhook = client.webhooks.create( form_id: 'YOUR_FORM_ID', url: 'https://your-app.com/webhook', enabled: true ) puts "Webhook created with ID: #{webhook.id}"
Handling the payload is up to you, but here's a taste:
post '/webhook' do payload = JSON.parse(request.body.read) # Do something awesome with the payload status 200 end
Always be prepared for the unexpected:
begin client.forms.get(form_id: 'NON_EXISTENT_ID') rescue Typeform::Errors::NotFoundError puts "Oops! That form doesn't exist." rescue Typeform::Errors::RateLimitError puts "Whoa there, speedster! Slow down a bit." end
And remember, respect those rate limits. Your API key will thank you.
Need something off the beaten path? Custom requests are your friend:
response = client.request.get('forms/YOUR_FORM_ID/responses', query: { completed: true })
Got a bunch of operations? Batch 'em up:
results = client.request.batch do |batch| batch.get('forms/FORM_ID_1') batch.get('forms/FORM_ID_2') batch.post('forms', body: { title: 'New Form' }) end
And there you have it! You're now armed and dangerous with Typeform API knowledge. Remember, this is just the tip of the iceberg. There's so much more you can do, so don't be afraid to experiment and push the boundaries.
Happy coding, and may your forms be ever awesome! 🚀