Step 1: Setting Up Your Odoo REST API – A Beginner’s Guide
🚀 Introduction: Why Odoo REST API?
Imagine this: You’re running a business using Odoo, and everything is working smoothly—until you realize you need to connect Odoo with another app, like a custom mobile application or a third-party service. Manually transferring data is tedious and error-prone.
This is where Odoo’s REST API comes in! It allows different software systems to communicate seamlessly, automating workflows and saving you hours of manual work.
If you're new to APIs, don’t worry—this guide will walk you through setting up your Odoo REST API step by step, even if you're not a coding expert.
🔧 Step 1: Install the Required Modules
Before diving into API setup, you need two key modules:
base_rest
– The core module that enables REST API functionality in Odoo.datamodel
(optional but helpful) – Simplifies data handling.
How to Install:
- Go to Odoo Apps (or install via command line if self-hosting).
- Search for
base_rest
and install it. - If needed, install
datamodel
for better data structure management.
💡 Pro Tip: If you’re self-hosting Odoo, you can install these modules via:
pip install odoo-addon-base-rest
🐍 Step 2: A Little Python Magic
Now that the modules are installed, you’ll need to write a small Python script to define your API endpoints (the URLs where your API will receive requests).
Basic API Setup Example:
- Create a new module (or modify an existing one).
- Inside your module, create a
controllers/
folder (if it doesn’t exist). - Add a Python file (e.g.,
api_controller.py
) with the following code:
from odoo import http
from odoo.addons.base_rest.controllers import main
class MyAPIController(http.Controller):
@http.route('/api/my_first_endpoint', type='json', auth='user', methods=['POST'])
def my_first_api(self, **kwargs):
return {"status": "success", "message": "Hello, API World!"}
What This Code Does:
- Creates an API endpoint at
/api/my_first_endpoint
. - Only accepts POST requests.
- Requires user authentication (
auth='user'
). - Returns a simple JSON response.
🔹 Test Your API:
Use Postman or cURL to send a POST request to:
curl -X POST http://your-odoo-server.com/api/my_first_endpoint -H "Content-Type: application/json" -d '{}'
If successful, you’ll see:
{"status": "success", "message": "Hello, API World!"}
📌 Step 3: Extending Your API (Optional but Powerful)
Once your basic API works, you can expand it to:
✔ Fetch Odoo data (e.g., list products, customers).
✔ Create/update records (e.g., add new sales orders via API).
✔ Integrate with external apps (e.g., Shopify, WhatsApp).
Example: Fetching Product Data
@http.route('/api/get_products', type='json', auth='user', methods=['GET'])
def get_products(self, **kwargs):
products = http.request.env['product.product'].search([])
return {"products": [{"name": p.name, "price": p.list_price} for p in products]}
Now, calling /api/get_products
will return a list of all products with names and prices.
🚀 Conclusion: Your API is Ready!
Congratulations! You’ve just set up your first Odoo REST API. 🎉
Next Steps:
- Explore more API endpoints (e.g., for invoices, contacts).
- Secure your API with rate limiting & API keys.
- Connect Odoo with external tools (Zapier, Make.com).
💬 Need Help?
If you’re stuck or want a deeper dive into Odoo APIs, drop an "API" in the comments, and we’ll guide you further!
🔹 Question for You:
What’s the first integration you’d build with Odoo’s API? Let us know below! 👇
📌 Key Takeaways
✅ Install base_rest
module for Odoo API functionality.
✅ Write a simple Python controller to define API endpoints.
✅ Test with Postman/cURL before full integration.
✅ Expand your API to fetch, create, or update Odoo data.
Now go ahead and automate your workflows like a pro!