Developing with Ayon's REST api

I’m currently working on some code that requires me to use Ayon’s REST api.
I’m using HTTPX for my REST calls but using requests works as well.

Initial login:

import httpx

payload = {"name": ayon_login_username, "password": ayon_login_password}

response = httpx.post(
    f"{ayon_server_url}/api/auth/login",
    json=payload,
)

token = response.json().get("token")

Get all projects:

headers = {}
headers[
    "Authorization"
] = f"Bearer {token}"

response = httpx.get(
    f"{ayon_server_url}/api/projects",
    headers=headers,
)

print(response.json())

You can also use a UserEntity as the authentication of a REST call:

import httpx
from ayon_server.auth.session import Session

session = await Session.create(user)

headers = {}
headers["Authorization"] = f"Bearer {session.token}"

async with httpx.AsyncClient() as client:
    response = await client.get(
        f"{ayon_server_url}/api/projects",
        headers=headers,
    )

print(response.json())

Some good info I figured out on where to place your payload:
On post the payload goes into json(and needs to be a json string)
On patch the payload goes into json (and needs to be a json string)
On put the payload goes into content

4 Likes

I highly recommend for experiments with HTTP API Postman https://www.postman.com/

Token got from login method could be added as Bearer Token in Authorization tab.

(Eg. you need to do first login and re-use returned token, which has validity for couple of hours, not sure how many. Google for Postman variables to make re-use of token easier.)

2 Likes

Because I’m a VS code Guy :nerd_face:
I like to use Thunder Client

3 Likes