Hey there, fellow developer! Ready to dive into the world of Hotmart API integration? You're in the right place. We'll be walking through the process of building a robust integration using Ruby. Hotmart's API is a powerful tool for managing digital products, and with Ruby's elegance, we'll make it sing.
Before we jump in, make sure you've got:
httparty
and dotenv
gemsLet's get our hands dirty:
mkdir hotmart_integration cd hotmart_integration bundle init
Add these to your Gemfile:
gem 'httparty' gem 'dotenv'
Then run:
bundle install
Hotmart uses OAuth 2.0. Here's a quick way to get your access token:
require 'httparty' require 'dotenv/load' class HotmartAuth include HTTParty base_uri 'https://api-sec-vlc.hotmart.com' def self.get_token response = post('/security/oauth/token', body: { grant_type: 'client_credentials', client_id: ENV['HOTMART_CLIENT_ID'], client_secret: ENV['HOTMART_CLIENT_SECRET'] }, headers: { 'Content-Type' => 'application/x-www-form-urlencoded' } ) response['access_token'] end end
Pro tip: Store your credentials in a .env
file and don't forget to add it to .gitignore
!
Now that we're authenticated, let's make some requests:
class HotmartAPI include HTTParty base_uri 'https://developers.hotmart.com/payments/api/v1' def initialize @options = { headers: { 'Authorization' => "Bearer #{HotmartAuth.get_token}" } } end def get_sales self.class.get('/sales/summary', @options) end end
Let's add some more methods to our HotmartAPI
class:
def get_product(id) self.class.get("/products/#{id}", @options) end def get_subscriptions self.class.get('/subscriptions', @options) end
Hotmart can send you real-time updates. Here's a basic Sinatra setup:
require 'sinatra' post '/webhook' do payload = JSON.parse(request.body.read) # Process the webhook payload status 200 end
Remember to set up your webhook URL in your Hotmart dashboard!
Always expect the unexpected:
def api_request yield rescue HTTParty::Error => e logger.error "API request failed: #{e.message}" nil end
Don't forget to test! Here's a simple RSpec example:
RSpec.describe HotmartAPI do it "fetches sales data" do api = HotmartAPI.new expect(api.get_sales).to be_a(Hash) end end
And there you have it! You've just built a solid foundation for your Hotmart API integration in Ruby. Remember, this is just the beginning. Explore the API docs, experiment with different endpoints, and most importantly, have fun building!
Got questions? Hit up the Hotmart developer community. Happy coding!