Back

Step by Step Guide to Building an Odoo ERP API Integration in JS

Aug 18, 20246 minute read

Introduction

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!

Prerequisites

Before we jump in, make sure you've got:

  • Node.js and npm (you're a pro, so I'm sure you've got this covered)
  • An Odoo instance (local or remote, dealer's choice)
  • API credentials (you know, the usual suspects: URL, database, username, and password)

Setting up the project

Let's get this party started:

mkdir odoo-integration && cd odoo-integration npm init -y npm install odoo-await

Configuring the Odoo client

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); } })();

Basic CRUD operations

Create a record

const newPartner = await odoo.create('res.partner', { name: 'John Doe', email: '[email protected]' }); console.log('New partner ID:', newPartner);

Read records

const partners = await odoo.search_read('res.partner', [['customer', '=', true]], ['name', 'email'] ); console.log('Customers:', partners);

Update a record

await odoo.update('res.partner', newPartner, { phone: '123-456-7890' }); console.log('Partner updated!');

Delete a record

await odoo.delete('res.partner', newPartner); console.log('Partner deleted!');

Advanced queries

Using domain filters

const highValueCustomers = await odoo.search_read('res.partner', [ ['customer', '=', true], ['total_invoiced', '>', 10000] ], ['name', 'total_invoiced'] ); console.log('High-value customers:', highValueCustomers);

Sorting and limiting results

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);

Working with relationships

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}`); }

Creating records with relations

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);

Handling errors and exceptions

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 }

Best practices and optimization tips

  1. Use search_read instead of separate search and read calls when possible.
  2. Limit the fields you request to improve performance.
  3. Use appropriate indexing on your Odoo database for frequently queried fields.
  4. Implement proper error handling and logging.
  5. Use connection pooling for high-traffic applications.

Conclusion and next steps

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!