Back

Step by Step Guide to Building an Odoo CRM API Integration in Go

Aug 18, 20248 minute read

Introduction

Hey there, fellow developer! Ready to dive into the world of Odoo CRM API integration using Go? You're in for a treat. Odoo CRM is a powerful tool, and its API opens up a whole new realm of possibilities. Whether you're looking to automate processes, sync data, or build custom applications, this guide will set you on the right path.

Prerequisites

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

  • Go installed on your machine (if not, head over to golang.org and sort that out)
  • An Odoo CRM account with API access (you'll need this for authentication)

Got those? Great! Let's get our hands dirty.

Setting up the Go project

First things first, let's set up our Go project:

mkdir odoo-crm-integration cd odoo-crm-integration go mod init github.com/yourusername/odoo-crm-integration

Now, let's grab the dependencies we'll need:

go get github.com/kolo/xmlrpc

Authentication

Alright, time to get cozy with Odoo's authentication. You'll need your API credentials handy for this part.

package main import ( "fmt" "github.com/kolo/xmlrpc" ) const ( url = "https://your-odoo-instance.odoo.com" db = "your-database" username = "your-username" password = "your-password" ) func main() { client, _ := xmlrpc.NewClient(url + "/xmlrpc/2/common", nil) var uid int err := client.Call("authenticate", []interface{}{db, username, password, map[string]string{}}, &uid) if err != nil { fmt.Println("Authentication failed:", err) return } fmt.Println("Authenticated successfully. User ID:", uid) }

Basic API Operations

Now that we're authenticated, let's look at some CRUD operations:

Reading data (GET)

func getCustomers(client *xmlrpc.Client, uid int) { var result []interface{} err := client.Call("execute_kw", []interface{}{ db, uid, password, "res.partner", "search_read", []interface{}{[]interface{}{[]string{"is_company", "=", true}}}, map[string]interface{}{"fields": []string{"name", "country_id", "comment"}}, }, &result) if err != nil { fmt.Println("Error:", err) return } fmt.Println("Customers:", result) }

Creating records (POST)

func createCustomer(client *xmlrpc.Client, uid int) { var id int err := client.Call("execute_kw", []interface{}{ db, uid, password, "res.partner", "create", []interface{}{map[string]interface{}{ "name": "New Customer", "is_company": true, }}, }, &id) if err != nil { fmt.Println("Error:", err) return } fmt.Println("Created customer with ID:", id) }

Updating records (PUT)

func updateCustomer(client *xmlrpc.Client, uid int, id int) { var result bool err := client.Call("execute_kw", []interface{}{ db, uid, password, "res.partner", "write", []interface{}{[]int{id}, map[string]interface{}{ "name": "Updated Customer Name", }}, }, &result) if err != nil { fmt.Println("Error:", err) return } fmt.Println("Update successful:", result) }

Deleting records (DELETE)

func deleteCustomer(client *xmlrpc.Client, uid int, id int) { var result bool err := client.Call("execute_kw", []interface{}{ db, uid, password, "res.partner", "unlink", []interface{}{[]int{id}}, }, &result) if err != nil { fmt.Println("Error:", err) return } fmt.Println("Delete successful:", result) }

Handling Odoo's XML-RPC

As you've probably noticed, we're using XML-RPC to communicate with Odoo. The github.com/kolo/xmlrpc package makes this a breeze. Just remember, all your method calls will follow this pattern:

client.Call("method_name", []interface{}{arguments}, &result)

Error Handling and Logging

Don't forget to implement robust error handling! Here's a quick example:

if err != nil { log.Printf("Error occurred: %v", err) // Handle the error appropriately return }

Advanced Features

Pagination

func getCustomersPaginated(client *xmlrpc.Client, uid int, offset, limit int) { var result []interface{} err := client.Call("execute_kw", []interface{}{ db, uid, password, "res.partner", "search_read", []interface{}{[]interface{}{[]string{"is_company", "=", true}}}, map[string]interface{}{ "fields": []string{"name", "country_id", "comment"}, "offset": offset, "limit": limit, }, }, &result) // Handle error and result }

Filtering and searching

func searchCustomers(client *xmlrpc.Client, uid int, domain []interface{}) { var ids []int err := client.Call("execute_kw", []interface{}{ db, uid, password, "res.partner", "search", []interface{}{domain}, }, &ids) // Handle error and ids }

Testing

Don't forget to write tests! Here's a simple example:

func TestAuthentication(t *testing.T) { client, _ := xmlrpc.NewClient(url+"/xmlrpc/2/common", nil) var uid int err := client.Call("authenticate", []interface{}{db, username, password, map[string]string{}}, &uid) if err != nil { t.Errorf("Authentication failed: %v", err) } if uid == 0 { t.Error("Expected non-zero user ID") } }

Best Practices

  1. Rate limiting: Be kind to Odoo's servers. Implement rate limiting in your requests.
  2. Caching: Cache frequently accessed data to reduce API calls.
  3. Security: Never expose your API credentials. Use environment variables or secure vaults.

Conclusion

And there you have it! You're now equipped to build a robust Odoo CRM API integration in Go. Remember, practice makes perfect, so don't be afraid to experiment and build upon these examples.

For more advanced topics, check out the official Odoo API documentation. Happy coding, and may your integrations be ever smooth and bug-free!