Back

Step by Step Guide to Building a Mercado Libre API Integration in Ruby

Aug 11, 20246 minute read

Introduction

Hey there, fellow developer! Ready to dive into the world of Mercado Libre API integration? You're in for a treat. Mercado Libre's API is a powerhouse for e-commerce in Latin America, and mastering it can open up a world of possibilities for your projects. Let's get our hands dirty and build something awesome!

Prerequisites

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

  • A Ruby environment set up (I know you've probably got this covered)
  • A Mercado Libre developer account (if you don't have one, go grab it real quick)
  • The httparty and oauth2 gems installed (we'll be using these to make our lives easier)

Authentication

First things first, let's get you authenticated:

  1. Head over to your Mercado Libre developer dashboard and snag your API credentials.
  2. Now, let's implement the OAuth 2.0 flow:
require 'oauth2' client = OAuth2::Client.new(YOUR_CLIENT_ID, YOUR_CLIENT_SECRET, site: 'https://api.mercadolibre.com') token = client.password.get_token(YOUR_USERNAME, YOUR_PASSWORD)

Boom! You're in. Keep that token safe; we'll need it.

Basic API Requests

Now that we're authenticated, let's make some requests:

require 'httparty' class MercadoLibreAPI include HTTParty base_uri 'https://api.mercadolibre.com' def initialize(token) @token = token end def get(endpoint) self.class.get(endpoint, headers: { 'Authorization' => "Bearer #{@token}" }) end end api = MercadoLibreAPI.new(token.token) response = api.get('/users/me') puts response.body

Easy peasy! You've just made your first API call.

Key Endpoints and Functionalities

Let's explore some key endpoints:

response = api.get('/sites/MLB/search?q=iphone')

Retrieving Item Details

response = api.get('/items/MLB1234567890')

Managing Orders

response = api.get('/orders/123456789')

Handling User Data

response = api.get('/users/123456789')

Error Handling and Rate Limiting

Always be prepared for the unexpected:

def get(endpoint) response = self.class.get(endpoint, headers: { 'Authorization' => "Bearer #{@token}" }) case response.code when 200 response.parsed_response when 429 raise "Rate limit exceeded. Try again later." else raise "Error: #{response.code} - #{response.message}" end end

And remember, play nice with the API. Respect those rate limits!

Data Processing and Storage

Once you've got your data, don't forget to parse and store it:

products = JSON.parse(response.body) products.each do |product| # Store in your database Product.create(name: product['title'], price: product['price']) end

Advanced Features

Ready to level up? Let's implement webhooks:

post '/webhook' do payload = JSON.parse(request.body.read) # Process the webhook payload status 200 end

Testing and Debugging

Always test your integration:

require 'minitest/autorun' class TestMercadoLibreAPI < Minitest::Test def setup @api = MercadoLibreAPI.new(TOKEN) end def test_get_user response = @api.get('/users/me') assert_equal 200, response.code end end

Best Practices and Optimization

  • Keep your code modular and DRY
  • Use environment variables for sensitive data
  • Cache responses when appropriate to reduce API calls

Conclusion

And there you have it! You've just built a solid Mercado Libre API integration in Ruby. Remember, this is just the beginning. Keep exploring, keep building, and most importantly, keep having fun with it. The e-commerce world is your oyster now. Go make something amazing!