Hey there, fellow JavaScript devs! Ready to dive into the world of Odoo ERP API? Let's get our hands dirty with some data syncing for user-facing integrations. Buckle up!
Odoo is a beast of an ERP system, and its API is your golden ticket to seamless integrations. Whether you're pulling in customer data or pushing out inventory updates, mastering this API is crucial for keeping your user-facing apps in perfect harmony with Odoo.
First things first, let's get our environment ready. You'll need the xmlrpc
package, so go ahead and install it:
npm install xmlrpc
Now, let's set up our connection parameters:
const xmlrpc = require('xmlrpc'); const config = { url: 'https://your-odoo-instance.com', db: 'your_database', username: 'your_username', password: 'your_password' };
Before we can do anything fun, we need to authenticate. Here's how you get that all-important session ID:
const common = xmlrpc.createClient({ host: config.url, port: 443, path: '/xmlrpc/2/common' }); common.methodCall('authenticate', [config.db, config.username, config.password, {}], (error, uid) => { if (error) { console.error('Authentication failed:', error); return; } console.log('Authenticated with user ID:', uid); // You're in! Now you can start making API calls });
Now that we're in, let's fetch some customer data:
const models = xmlrpc.createClient({ host: config.url, port: 443, path: '/xmlrpc/2/object' }); models.methodCall('execute_kw', [ config.db, uid, config.password, 'res.partner', 'search_read', [[['is_company', '=', true]]], { fields: ['name', 'country_id', 'comment'], limit: 5 } ], (error, result) => { if (error) { console.error('Failed to fetch customers:', error); return; } console.log('Customers:', result); });
Creating a new product? Easy peasy:
models.methodCall('execute_kw', [ config.db, uid, config.password, 'product.template', 'create', [{ 'name': 'Awesome Product', 'list_price': 99.99, 'type': 'consu' }] ], (error, id) => { if (error) { console.error('Failed to create product:', error); return; } console.log('New product created with ID:', id); });
Updating is just as simple:
models.methodCall('execute_kw', [ config.db, uid, config.password, 'product.template', 'write', [[id], { 'list_price': 89.99 }] ], (error, result) => { if (error) { console.error('Failed to update product:', error); return; } console.log('Product updated:', result); });
Odoo loves its relations. Here's how you create a sales order with line items:
models.methodCall('execute_kw', [ config.db, uid, config.password, 'sale.order', 'create', [{ 'partner_id': customer_id, 'order_line': [ [0, 0, { 'product_id': product_id, 'product_uom_qty': 1 }] ] }] ], (error, order_id) => { if (error) { console.error('Failed to create sales order:', error); return; } console.log('Sales order created with ID:', order_id); });
When you're dealing with large datasets, pagination is your friend:
function syncCustomers(offset = 0, limit = 100) { models.methodCall('execute_kw', [ config.db, uid, config.password, 'res.partner', 'search_read', [[['is_company', '=', true]]], { fields: ['name', 'country_id', 'comment'], offset: offset, limit: limit } ], (error, result) => { if (error) { console.error('Sync failed:', error); return; } processCustomers(result); if (result.length === limit) { syncCustomers(offset + limit, limit); } }); } syncCustomers();
Always be prepared for things to go sideways. Wrap your API calls in try-catch blocks and implement retry logic for transient errors. And remember, Odoo has rate limits, so be nice and don't hammer the API!
Odoo supports webhooks for real-time updates. While setting them up is beyond the scope of this article, know that they exist and can be super useful for keeping your integration up-to-date.
There you have it, folks! You're now armed with the knowledge to read and write data using the Odoo ERP API like a pro. Remember, practice makes perfect, so get out there and start building some awesome integrations!
Want to learn more? Check out the official Odoo API documentation and join the community forums. Happy coding!