Hey there, fellow developer! Ready to dive into the world of Odoo ERP API integration? You're in for a treat. We'll be using the odoo-await
package to make our lives easier and our code cleaner. Buckle up!
Before we jump in, make sure you've got:
Let's get this party started:
mkdir odoo-integration && cd odoo-integration npm init -y npm install odoo-await
Time to make some connections:
const Odoo = require('odoo-await'); const odoo = new Odoo({ baseUrl: 'http://your-odoo-instance.com', port: 8069, db: 'your_database', username: 'your_username', password: 'your_password' }); // Let's test the connection (async () => { try { await odoo.connect(); console.log('Connected to Odoo!'); } catch (error) { console.error('Oops! Connection failed:', error); } })();
const newPartner = await odoo.create('res.partner', { name: 'John Doe', email: '[email protected]' }); console.log('New partner ID:', newPartner);
const partners = await odoo.search_read('res.partner', [['customer', '=', true]], ['name', 'email'] ); console.log('Customers:', partners);
await odoo.update('res.partner', newPartner, { phone: '123-456-7890' }); console.log('Partner updated!');
await odoo.delete('res.partner', newPartner); console.log('Partner deleted!');
const highValueCustomers = await odoo.search_read('res.partner', [ ['customer', '=', true], ['total_invoiced', '>', 10000] ], ['name', 'total_invoiced'] ); console.log('High-value customers:', highValueCustomers);
const topCustomers = await odoo.search_read('res.partner', [['customer', '=', true]], ['name', 'total_invoiced'], { order: 'total_invoiced DESC', limit: 5 } ); console.log('Top 5 customers:', topCustomers);
const salesOrders = await odoo.search_read('sale.order', [['state', '=', 'sale']], ['name', 'partner_id', 'amount_total'] ); for (const order of salesOrders) { const partner = await odoo.read('res.partner', order.partner_id[0], ['name', 'email']); console.log(`Order ${order.name} for ${partner.name} (${partner.email}): ${order.amount_total}`); }
const productId = await odoo.create('product.product', { name: 'Awesome Product', list_price: 99.99 }); const orderId = await odoo.create('sale.order', { partner_id: newPartner, order_line: [ [0, 0, { product_id: productId, product_uom_qty: 1 }] ] }); console.log('New order created:', orderId);
Always wrap your Odoo operations in try-catch blocks:
try { // Your Odoo operations here } catch (error) { console.error('Error:', error.message); // Handle the error appropriately }
search_read
instead of separate search
and read
calls when possible.Congratulations! You've just leveled up your Odoo integration skills. From here, you can explore more complex operations, dive into custom modules, or even contribute to the odoo-await
package.
Remember, the Odoo API is vast and powerful. Don't be afraid to experiment and push its limits. Happy coding, and may your integrations be ever smooth!