Back

Step by Step Guide to Building a Hotmart API Integration in Ruby

Aug 15, 20245 minute read

Introduction

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.

Prerequisites

Before we jump in, make sure you've got:

  • Ruby 2.7+ installed
  • httparty and dotenv gems
  • Your Hotmart API credentials (if you don't have these, hop over to your Hotmart dashboard and grab them)

Setting up the project

Let'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

Authentication

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!

Making API requests

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

Implementing key Hotmart API endpoints

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

Webhook integration

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!

Error handling and logging

Always expect the unexpected:

def api_request yield rescue HTTParty::Error => e logger.error "API request failed: #{e.message}" nil end

Testing the integration

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

Best practices and optimization

  • Respect rate limits: Implement exponential backoff for retries
  • Cache responses when appropriate to reduce API calls
  • Use background jobs for webhook processing if you're handling high volumes

Conclusion

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!