Chapter 14 - Odoo 15 Development Book

Generate API Key:

With Odoo 14, Odoo has introduced the support for API keys required to perform web service operations.

Usually, while connecting to an Odoo database with the help of an external API such as XML-RPC, we use the username and password for authentication. But with the introduction of a new API key, we can use it to authenticate users using external APIs.

A user can generate multiple API keys. Below are the steps explained to generate an API key.

  • Go to Preferences inside the Profile on top.
  • odoo-development-book
  • In the Account Security tab, you can find an option for generating an API key.
  • odoo-development-book
  • On clicking New API Key, you’ll be prompted to enter the user password.
  • odoo-development-book
  • After confirmation, you'll be asked to enter a description and purpose of the new API key.
  • odoo-development-book
  • On adding a description and clicking the Generate Key button, you’ll be provided with a new API key.
  • odoo-development-book

Keep in mind that once a key is generated, you won’t be able to retrieve it later. So make sure that you keep a copy of the key safe and secure somewhere else.

Also, be aware that this API key cannot be used to log in from the UI. This can only be used to access externally using webservices.

Multiple API keys can be generated under a single user which can be listed under the Account Security tab in the Preferences section. Also, the user is provided with an option for deleting the API key. The API key is lost forever and cannot be retrieved again on deletion. You’ll have to generate a new API key.

odoo-development-book

Let us check with an example of how we can use this API key to authenticate a user using the webservice.

import xmlrpc.client
url = 'http://0.0.0.0:8015'
database = 'DemoDB'
user = 'admin'
password = '84800371ad038b3b641c5db56e1313029deb5030'
common = xmlrpc.client.ServerProxy('{}/xmlrpc/2/common'.format(url))
version_db = common.version()
user = common.authenticate(database, user, password, {})
if user:
   print("User is authenticated....")
else:
   print("Authentication Failed. Please check the credentials")

In the above code, we have used the XML-RPC method to connect to the Odoo database. We have imported the xmlrpc.client python package.

Next, we have provided the URL, database, and user credentials. And for the password section, we have provided the new API key instead of the original password.

After that, we called the connection methods and authenticated the user. If all the credentials were correct, the user will be logged in and we’ll get a successful print message.

whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message