Hey there, fellow developer! Ready to dive into the world of Qwilr API integration? You're in for a treat. Qwilr's API is a powerful tool that lets you create, manage, and customize beautiful web-based documents programmatically. In this guide, we'll walk through building a robust integration in Ruby. Let's get cracking!
Before we jump in, make sure you've got:
httparty
gem (we'll use this for API requests)First things first, let's get our environment ready:
gem install httparty
Now, let's set up our API credentials. Create a .env
file in your project root:
QWILR_API_KEY=your_api_key_here
QWILR_ACCOUNT_ID=your_account_id_here
Time to test the waters! Let's create a basic client and make a test request:
require 'httparty' require 'dotenv/load' class QwilrClient include HTTParty base_uri 'https://api.qwilr.com/v1' def initialize @options = { headers: { 'X-Api-Key' => ENV['QWILR_API_KEY'], 'X-Account-Id' => ENV['QWILR_ACCOUNT_ID'] } } end def test_connection self.class.get('/user', @options) end end client = QwilrClient.new response = client.test_connection puts response.code == 200 ? "Connection successful!" : "Oops, something went wrong."
If you see "Connection successful!", you're golden!
Now that we're connected, let's implement some core features:
def create_page(title, content) self.class.post('/pages', @options.merge( body: { title: title, sections: [{ type: 'text', content: content }] }.to_json )) end
def get_page(page_id) self.class.get("/pages/#{page_id}", @options) end
def update_page(page_id, title, content) self.class.put("/pages/#{page_id}", @options.merge( body: { title: title, sections: [{ type: 'text', content: content }] }.to_json )) end
def delete_page(page_id) self.class.delete("/pages/#{page_id}", @options) end
Ready to level up? Let's tackle some advanced features:
def create_from_template(template_id, title) self.class.post('/pages', @options.merge( body: { title: title, template_id: template_id }.to_json )) end
require 'sinatra' post '/webhook' do payload = JSON.parse(request.body.read) # Process the webhook payload status 200 end
def get_all_pages(page = 1, per_page = 20) self.class.get('/pages', @options.merge(query: { page: page, per_page: per_page })) end
Always expect the unexpected! Here's a simple error handling wrapper:
def api_request response = yield if response.success? response else raise "API Error: #{response.code} - #{response.message}" end end
Use it like this:
api_request { get_page(page_id) }
Remember to implement rate limiting and follow Qwilr's API usage guidelines to keep your integration running smoothly.
Don't forget to test! Here's a quick example using RSpec:
RSpec.describe QwilrClient do let(:client) { QwilrClient.new } it "creates a new page" do response = client.create_page("Test Page", "This is a test page") expect(response.code).to eq(201) end end
When deploying, make sure to:
And there you have it! You've just built a solid Qwilr API integration in Ruby. From basic connections to advanced features, you're now equipped to create, manage, and customize Qwilr pages programmatically.
Remember, the Qwilr API is powerful and flexible. Don't be afraid to experiment and push the boundaries of what you can create. Happy coding!