How to use Odoo API with Node.js

How to use Odoo API with Node.js

Odoo is a suite of open-source business applications that includes customer relationship management (CRM), sales, project management, manufacturing, inventory management, and other business-related functions. It is known for its modular approach, allowing users to start with basic functionalities and add new modules as their needs grow. The platform also offers both cloud-based and on-premises deployment options, providing flexibility to businesses of various sizes.

To use Odoo API with Node.js, you can follow these steps.

Get your API key by following the official documentation:

https://www.odoo.com/documentation/16.0/developer/reference/external_api.html#api-keys

Install the xmlrpc package using npm:

npm install xmlrpc

And then use the following snippet to execute some requests:

const xmlrpc = require('xmlrpc');

// Odoo credentials
const odooUrl = 'https://mycompany.odoo.com/xmlrpc/2/common';
const db = 'mycompany';
const username = 'my@user.com';
const password = 'my_api_key';

// Create an XML-RPC client
const client = xmlrpc.createClient({ url: odooUrl });

// wrapper to execute methods
const exec = (method, params) => new Promise((resolve, reject) => {
    client.methodCall(method, params, (error, value) =>{
        console.info('executing Odoo method', method);
        if (error) reject(error)
        else resolve(value)
    })
});

// some shortcuts
const version = () => exec('version', []);
const auth = () => exec('authenticate', [db, username, password, {}]);

// execute some requests
const index = async () => {
    const v = await version();
    console.info(v)

    const a = await auth();
    console.info(a); // user id
}

// do execute
index().then(result => console.info(result)).catch(err => console.error(err));