ERP

    Odoo API Integration: XML-RPC, JSON-RPC, and REST API

    Odoo exposes XML-RPC and JSON-RPC APIs for integration with external applications. This developer guide covers authentication, CRUD operations, search queries, and integration patterns.

    VT
    12 min read
    Share

    Key Takeaways

    • Odoo exposes XML-RPC and JSON-RPC APIs for full CRUD access to every model and method in the system
    • Use API keys instead of passwords for production integrations to improve security and avoid credential rotation issues
    • search_read combines search and read in a single call, reducing round trips and improving integration performance
    • Odoo domain syntax (field, operator, value tuples) powers all filtering and must be mastered for efficient queries
    • REST API is available in Enterprise edition natively, or via third-party modules for Community edition

    API Protocols Comparison

    Odoo supports three API protocols. XML-RPC is the oldest and most documented, JSON-RPC is the internal protocol used by the web client, and REST API is the modern option available in Enterprise.

    ProtocolFormatAuthEditionUse Case
    XML-RPCXMLPassword/API keyAllLegacy integrations, wide library support
    JSON-RPCJSONSession cookieAllWeb apps, internal API calls
    REST APIJSONBearer tokenEnterpriseModern integrations, mobile apps
    Recommendation

    For new integrations, use JSON-RPC for Community edition or REST API for Enterprise. XML-RPC works but is verbose and slower due to XML serialization overhead.

    Authentication and Security

    Every API call requires authentication. The method varies by protocol but always involves identifying the database, user, and credential. API keys are strongly recommended over passwords.

    1. Generate an API key: Settings > Users > select user > API Keys tab > New API Key
    2. Store keys securely in environment variables, never in source code
    3. Use the authenticate endpoint to obtain a user ID (uid) for subsequent calls
    4. For JSON-RPC, maintain session cookies across requests
    5. Implement token refresh and reconnection logic for long-running integrations
    IP Whitelisting

    Restrict API access to known IP addresses using Odoo's proxy or firewall rules. Open API endpoints are a common attack vector for brute-force authentication attempts.

    CRUD Operations and Domain Filters

    All data manipulation follows the same pattern: specify the model name, method, and arguments. The four core methods are search_read, create, write, and unlink.

    MethodPurposeKey Arguments
    search_readQuery recordsdomain, fields, limit, offset, order
    createInsert new recordfield values dict
    writeUpdate recordsrecord IDs, field values dict
    unlinkDelete recordsrecord IDs

    Domain filter examples: [('state', '=', 'sale')] finds confirmed sales orders. [('amount_total', '>', 1000), ('partner_id.country_id.code', '=', 'US')] finds large US orders. Combine with '|' operator for OR logic.

    Handling Relational Fields

    Many-to-one, one-to-many, and many-to-many fields use special command syntax for write operations. This syntax is the most common source of confusion for developers new to the Odoo API.

    CommandSyntaxEffect
    Link(4, id, 0)Add existing record to relation
    Unlink(3, id, 0)Remove from relation without deleting
    Delete(2, id, 0)Remove from relation and delete record
    Create(0, 0, values)Create new record and link
    Replace(6, 0, [ids])Replace all linked records with new set
    Order Lines Example

    To add a line to a sales order: write(order_id, {'order_line': [(0, 0, {'product_id': 1, 'product_uom_qty': 5})]}) creates a new line and links it automatically.

    Performance Optimization

    API performance depends on network latency, query complexity, and result set size. Batch operations and selective field loading have the largest impact on throughput.

    • Request only needed fields to reduce payload size and database load
    • Use limit and offset for pagination instead of fetching all records
    • Batch create/write operations: pass lists of values instead of looping individual calls
    • Cache the authenticated uid to avoid re-authenticating on every request
    • Use search_count before search_read to show totals without loading full records
    • Enable server-side caching for read-heavy integrations with infrequently changing data

    Frequently Asked Questions

    Need Help Implementing This?

    Our team can help you put these insights into practice.

    Schedule a Consultation