Hey there, fellow developer! Ready to dive into the world of Zoho Bookings API integration? You're in for a treat. This guide will walk you through building a robust integration using Ruby, allowing you to tap into the power of Zoho's scheduling system. Let's get cracking!
Before we jump in, make sure you've got:
Got those? Great! Let's move on.
First things first, let's get our project off the ground:
mkdir zoho_bookings_integration cd zoho_bookings_integration bundle init
Now, open up that Gemfile and add these gems:
gem 'httparty' gem 'dotenv'
Run bundle install
, and we're ready to rock!
Alright, time to get that access token. Create a .env
file in your project root:
ZOHO_CLIENT_ID=your_client_id
ZOHO_CLIENT_SECRET=your_client_secret
ZOHO_REFRESH_TOKEN=your_refresh_token
Now, let's write a method to get and refresh our access token:
require 'httparty' require 'dotenv/load' def get_access_token response = HTTParty.post('https://accounts.zoho.com/oauth/v2/token', body: { refresh_token: ENV['ZOHO_REFRESH_TOKEN'], client_id: ENV['ZOHO_CLIENT_ID'], client_secret: ENV['ZOHO_CLIENT_SECRET'], grant_type: 'refresh_token' }) JSON.parse(response.body)['access_token'] end
Time to make our first API call! Let's fetch some bookings:
def get_bookings access_token = get_access_token response = HTTParty.get('https://bookings.zoho.com/api/v1/json/bookings', headers: { 'Authorization' => "Zoho-oauthtoken #{access_token}" }) JSON.parse(response.body) end
Now for the fun part - let's implement some core booking functions:
def create_booking(service_id, start_time, end_time, customer_details) access_token = get_access_token response = HTTParty.post('https://bookings.zoho.com/api/v1/json/bookings', headers: { 'Authorization' => "Zoho-oauthtoken #{access_token}" }, body: { service_id: service_id, start_time: start_time, end_time: end_time, customer_details: customer_details }.to_json) JSON.parse(response.body) end def cancel_booking(booking_id) access_token = get_access_token response = HTTParty.delete("https://bookings.zoho.com/api/v1/json/bookings/#{booking_id}", headers: { 'Authorization' => "Zoho-oauthtoken #{access_token}" }) JSON.parse(response.body) end
Let's not forget about error handling. Wrap your API calls in a begin/rescue block:
begin result = create_booking(service_id, start_time, end_time, customer_details) rescue => e puts "Oops! Something went wrong: #{e.message}" end
Want to level up? Let's add webhook support for real-time updates:
require 'sinatra' post '/webhook' do payload = JSON.parse(request.body.read) # Process the webhook payload puts "Received webhook: #{payload}" status 200 end
Don't forget to test your integration! Here's a quick example using RSpec:
require 'rspec' require_relative 'your_integration_file' RSpec.describe 'Zoho Bookings Integration' do it 'successfully creates a booking' do result = create_booking('service123', '2023-06-01T10:00:00', '2023-06-01T11:00:00', { name: 'John Doe', email: '[email protected]' }) expect(result['booking_id']).not_to be_nil end end
Remember to:
.env
file to version controlAnd there you have it! You've just built a solid Zoho Bookings API integration in Ruby. Pretty cool, right? Remember, this is just the beginning. There's a whole world of possibilities with the Zoho Bookings API. Keep exploring, keep coding, and most importantly, have fun with it!
Got questions? Hit up the Zoho Bookings API docs or dive into the Ruby community. Happy coding!