I need help getting the unique identifier for booked meetings in Calendly. The issue is that users schedule appointments through a booking_url redirect instead of my API, so I can’t capture the UUID during the booking process.
require 'net/http'
class SchedulingController < ApplicationController
before_action :authenticate_user
def fetch_event_types
owner_uri = ENV['CALENDLY_OWNER_URI']
api_endpoint = "https://api.calendly.com/event_types?user=#{owner_uri}"
request_headers = {
'Accept' => 'application/json',
'Authorization' => "Bearer #{ENV['CALENDLY_ACCESS_TOKEN']}"
}
api_response = Net::HTTP.get_response(URI(api_endpoint), request_headers)
parsed_data = JSON.parse(api_response.body)
render json: parsed_data
end
end
The API returns data like this:
{
"collection": [{
"uri": "https://api.calendly.com/event_types/BBBBBBBBBBBBBBBB",
"name": "30 Min Consultation",
"active": true,
"booking_method": "standard",
"slug": "consultation-call",
"scheduling_url": "https://calendly.com/consultation-call",
"duration": 30,
"kind": "solo",
"type": "StandardEventType",
"color": "#0069ff",
"created_at": "2023-05-15T10:30:25.123456Z",
"updated_at": "2023-06-20T14:22:18.987654Z",
"description_plain": "Book a 30 minute consultation",
"description_html": "<p>Book a 30 minute consultation</p>",
"profile": {
"type": "User",
"name": "Sarah Wilson",
"owner": "https://api.calendly.com/users/BBBBBBBBBBBBBBBB"
}
}]
}
Since appointments get created through the scheduling_url redirect rather than my application, I want to call https://api.calendly.com/scheduled_events/{uuid} to get event details for my database.
I also need to create records in my Session model after each successful booking. Right now the booking happens outside my app through Calendly’s interface. I’m thinking about fetching event data and using it to populate my database automatically. Is there a better approach?
Here are my model relationships:
class Therapist < ApplicationRecord
has_many :sessions
has_many :clients, through: :sessions
end
class Session < ApplicationRecord
belongs_to :therapist
belongs_to :client
has_one :note
end
class Client < ApplicationRecord
has_many :sessions
has_many :notes
has_many :therapists, through: :sessions
end
class Note < ApplicationRecord
belongs_to :session
belongs_to :client
end