Hey there, fellow developer! Ready to dive into the world of Magento 2 API integration? You're in for a treat. Magento 2's API is a powerful tool that can supercharge your e-commerce projects. Whether you're building a custom dashboard, syncing inventory, or creating a mobile app, this guide will get you up and running in no time.
Before we jump in, make sure you've got:
requests
library (pip install requests
)Got all that? Great! Let's get coding.
First things first: we need to get that sweet, sweet access token. Here's how:
import requests url = "https://your-magento-store.com/rest/V1/integration/admin/token" data = { "username": "your_username", "password": "your_password" } response = requests.post(url, json=data) token = response.json() print(f"Your access token: {token}")
Pro tip: This token expires, so you might want to wrap this in a function that checks and refreshes as needed.
Now that we're authenticated, let's make some requests!
Fetching product info is a breeze:
headers = {"Authorization": f"Bearer {token}"} product_id = 1 url = f"https://your-magento-store.com/rest/V1/products/{product_id}" response = requests.get(url, headers=headers) product = response.json() print(f"Product name: {product['name']}")
Creating a new customer? Easy peasy:
url = "https://your-magento-store.com/rest/V1/customers" data = { "customer": { "email": "[email protected]", "firstname": "John", "lastname": "Doe" } } response = requests.post(url, json=data, headers=headers) new_customer = response.json() print(f"New customer ID: {new_customer['id']}")
Always check your responses! Magento 2 returns JSON, so let's parse it:
if response.status_code == 200: data = response.json() # Do something with the data else: print(f"Oops! Error: {response.status_code}") print(response.text)
Dealing with large datasets? Pagination's got your back:
url = "https://your-magento-store.com/rest/V1/products" params = { "searchCriteria[pageSize]": 20, "searchCriteria[currentPage]": 1 } response = requests.get(url, params=params, headers=headers)
Want to get specific? Use filters:
params = { "searchCriteria[filter_groups][0][filters][0][field]": "price", "searchCriteria[filter_groups][0][filters][0][value]": "100", "searchCriteria[filter_groups][0][filters][0][condition_type]": "gt" }
Postman is your friend for quick API tests. For Python debugging, liberal use of print()
statements never hurt anyone!
Let's put it all together with a basic inventory management tool:
def get_low_stock_products(threshold=10): url = "https://your-magento-store.com/rest/V1/products" params = { "searchCriteria[filter_groups][0][filters][0][field]": "quantity_and_stock_status.qty", "searchCriteria[filter_groups][0][filters][0][value]": str(threshold), "searchCriteria[filter_groups][0][filters][0][condition_type]": "lt" } response = requests.get(url, params=params, headers=headers) products = response.json()['items'] for product in products: print(f"Low stock alert: {product['name']} - Qty: {product['quantity_and_stock_status']['qty']}") get_low_stock_products()
And there you have it! You're now equipped to build some seriously cool integrations with Magento 2's API. Remember, the official Magento 2 API documentation is your best friend for diving deeper.
Now go forth and code something awesome! 🚀