Hey there, fellow dev! Ready to dive into the world of Gumroad API integration? Let's get cracking!
Gumroad's API is a powerful tool that lets you tap into their e-commerce platform. Whether you're looking to fetch product info, manage sales data, or handle subscriptions, this guide will walk you through the process. Buckle up!
Before we jump in, make sure you've got:
Let's kick things off:
mkdir gumroad-integration && cd gumroad-integration npm init -y npm install axios dotenv
First things first, let's get that API key sorted:
.env
file in your project root:GUMROAD_API_KEY=your_api_key_here
.env
to your .gitignore
!Time to get our hands dirty with some code:
require('dotenv').config(); const axios = require('axios'); const gumroadApi = axios.create({ baseURL: 'https://api.gumroad.com/v2', headers: { 'Authorization': `Bearer ${process.env.GUMROAD_API_KEY}` } }); async function makeRequest(endpoint, method = 'GET', data = {}) { try { const response = await gumroadApi({ method, url: endpoint, data }); return response.data; } catch (error) { console.error('API request failed:', error.response.data); throw error; } }
Let's implement some key features:
// Fetch product info async function getProduct(productId) { return makeRequest(`/products/${productId}`); } // Retrieve sales data async function getSales(limit = 10) { return makeRequest('/sales', 'GET', { limit }); } // Manage subscriptions async function getSubscriptions(productId) { return makeRequest(`/products/${productId}/subscribers`); }
Gumroad can send you real-time updates. Here's a quick Express setup:
const express = require('express'); const app = express(); app.post('/webhook', express.json(), (req, res) => { const event = req.body; console.log('Received webhook:', event); // Handle the event based on its type res.sendStatus(200); }); app.listen(3000, () => console.log('Webhook server running on port 3000'));
Let's add some resilience to our code:
const { default: rateLimit } = require('axios-rate-limit'); const gumroadApi = rateLimit(axios.create({ // ... previous config }), { maxRequests: 100, perMilliseconds: 60000 }); async function makeRequest(endpoint, method = 'GET', data = {}, retries = 3) { try { return await gumroadApi({ method, url: endpoint, data }); } catch (error) { if (retries > 0 && error.response && error.response.status >= 500) { await new Promise(resolve => setTimeout(resolve, 1000)); return makeRequest(endpoint, method, data, retries - 1); } throw error; } }
Don't forget to test! Here's a simple Jest test to get you started:
jest.mock('axios'); test('getProduct fetches product data', async () => { const mockProduct = { id: '123', name: 'Awesome Product' }; axios.get.mockResolvedValue({ data: mockProduct }); const product = await getProduct('123'); expect(product).toEqual(mockProduct); });
And there you have it! You're now equipped to build a robust Gumroad API integration. Remember, the official Gumroad API docs are your best friend for diving deeper.
Now go forth and code something awesome! 🚀