Hey there, fellow developer! Ready to supercharge your project with Gumroad's powerful API? You're in the right place. In this guide, we'll walk through building a Gumroad API integration in Python. It's easier than you might think, and by the end, you'll be pulling product data, creating listings, and handling webhooks like a pro.
Before we dive in, make sure you've got:
We'll be using the requests
library, so if you haven't already, go ahead and install it:
pip install requests
First things first, let's get you authenticated. Head over to your Gumroad settings and grab your API key. Don't share this with anyone – it's your secret sauce!
Here's how to set up your authentication headers:
import requests API_KEY = 'your_api_key_here' headers = { 'Authorization': f'Bearer {API_KEY}', 'Content-Type': 'application/json' }
Now for the fun part – let's make some requests!
Want to fetch details about a product? Here's how:
product_id = 'your_product_id' response = requests.get(f'https://api.gumroad.com/v2/products/{product_id}', headers=headers) print(response.json())
Feeling creative? Let's create a new product:
new_product = { 'name': 'Awesome Python Guide', 'price': 10, 'description': 'The best guide for Python developers!' } response = requests.post('https://api.gumroad.com/v2/products', headers=headers, json=new_product) print(response.json())
Gumroad's API speaks JSON, so let's parse those responses:
if response.status_code == 200: data = response.json() print(f"Success! Product name: {data['product']['name']}") else: print(f"Oops! Something went wrong: {response.status_code}")
Got a lot of products? No sweat! Here's how to handle pagination:
def get_all_products(): url = 'https://api.gumroad.com/v2/products' all_products = [] while url: response = requests.get(url, headers=headers) data = response.json() all_products.extend(data['products']) url = data.get('next_page_url') return all_products
Want to get fancy with your queries? Try this:
params = { 'sort': 'created', 'after': '2023-01-01' } response = requests.get('https://api.gumroad.com/v2/products', headers=headers, params=params)
Let's set up a webhook to keep your app in the loop:
from flask import Flask, request app = Flask(__name__) @app.route('/webhook', methods=['POST']) def handle_webhook(): event = request.json if event['event'] == 'sale': print(f"New sale for product: {event['product_name']}") return '', 200 if __name__ == '__main__': app.run(port=5000)
Let's tie it all together with a basic product management system:
def list_products(): products = get_all_products() for product in products: print(f"{product['name']} - ${product['price']}") def create_product(name, price, description): new_product = { 'name': name, 'price': price, 'description': description } response = requests.post('https://api.gumroad.com/v2/products', headers=headers, json=new_product) if response.status_code == 200: print(f"Created new product: {name}") else: print("Failed to create product") # Usage list_products() create_product("New Python Course", 49.99, "Learn Python in 30 days!")
And there you have it! You're now equipped to harness the power of the Gumroad API in your Python projects. Remember, this is just scratching the surface – there's so much more you can do. Check out the Gumroad API documentation for more details and happy coding!
Don't be afraid to experiment and build something awesome. The possibilities are endless, and who knows? Your next big project might just be a Gumroad integration away. Keep coding, keep learning, and most importantly, have fun!