How to find/create API routes to communicate with Tryton-Database

Hello,
I am new to tryton and I have managed to start the tryton server and tryton-sao following the documentation.

Now I want to connect the backend of tryton with react-native.
In this case, I need to get API’s to communicate to the backend server.

I went through the forums and documentation but didn’t manage to understand it clearly.

Are there already defined API routes?

How do I access them and use them?

Please tell me all the steps in a bit of detail, because am learning it.

Thanks

By default there is only the JSON-RPC and XML-RPC api. This is the generic API that the clients used. (It is not yet fully documented but available methods are defined in __rpc__ attribute of each object).

But usually if you create a dedicated client, you will want to define your own API for it. This can be done per module by registering route: User Application — Tryton server. But you must be careful to manage authentication, access etc. using the provided wrappers.

I tried searching for __rpc__ but unable to find it. Which file should I look for?

How do I register the route? In which file? It might sound silly but I am really confused.

Thanks Ced

A grep in the source files show plenty on __rpc__ definition. trytond.model.Model has some defined by default.

It is just a matter to apply decorator on the method and ensure that the file where the method is defined is imported by __init__.py. The Tryton convention is to put them in a routes.py file and to import it in __init__.py like in the timesheet module.

Thanks for helping me.

So now I have created a custom module (jay_module) and added a test.js file with an app route. But I am getting 405 Method Not Allowed .

On GET request of localhost:8000/hello

BELOW IS WHAT I DID

#test.py
from trytond.application import app

@app.route('/hello', methods=['GET'])
def hello(request):
    return 'Hello world'

and __init__.py

#__init__.py
from trytond.pool import Pool
from test import *
__all__ = ['register']
def register():
    Pool.register(
        module='jay_module', type_='model')
    Pool.register(
        module='jay_module', type_='wizard')
    Pool.register(
        module='jay_module', type_='report')

and tryton.cfg

[tryton]
version=5.6.1
depends:
    ir
xml:

After that, I installed the module and updated the module list also enabled it in Administration. Then I tried running in postman and got this error. 405 Method Not Allowed

Thanks

I do not know how this could be valid Python code but this should be:

from trytond.wsgi import app

@app.route('/hello', methods=['GET'])
def hello(request):
    return "Hello world"

Thanks ced,
I am sorry, I pasted the code incorrectly on the forum. But now I corrected it and replaced the package import in my files too. I also reinstalled the module and updated it in modules-list.

Still I am getting the same error of 405 Method not Allowed. Am I missing any step? I don’t know.

I guess the file is not imported at startup and so the route is not registered.

Are you talking about __init__.py file of the module?

I’m talking about all the python files in your module.

Yes I corrected the test.py file importing of hello function. But still same problem.

from trytond.pool import Pool

from .test import hello

__all__ = ['register']

def register():

    Pool.register(

        module='jay_module', type_='model')

    Pool.register(

        module='jay_module', type_='wizard')

    Pool.register(

        module='jay_module', type_='report')

Then your module is not loaded.

Thanks, ced for being so helpful.

What is the correct way of loading the module?

It’s working now!
I upgraded all the modules from the admin panel and It worked.

Now If I want to use an existing module(provided by tryton), do I need to register routes on top of each individual method in the module file?

Thanks

@ced

I think I can talk on behalf of those, who want to add simple user application, extracting just some basic data from Tryton, without reading and learning the whole source code. The User Application — trytond latest documentation is good starting point, but an example showing how data can be extracted from Tryton would be great. I know this is a job someone experienced in Tryton has to do with no direct benefit, but I think there may be indirect benefits even for the Tryton gurus in long term.

5 Likes

I have the same issue, using a very similar module. The module is imported only after I performed some request using the GUI client. When restarting the server the module is not imported and the endpoint is not reachable.

(I added a print statement into the module so I can see when it is imported.)

So this comes down to the question: How can I make trytond import all activated modules on start?

You must specify database param: -d <db_name>

2 Likes

This did the trick! Many thanks.