AYON REST API Guide

Acknowledgement

Many thanks to @Danell
His posts encouraged me to create this guide.

Disclaimer

This post aims to scratch under the surface of ayon-python-api
where you learn about how to make the requests yourself.
If you want a simpler guide, go to AYON Python API First Steps

TL;DR

Go to the Extended Example Code section.

Intro

AYON API allows communication with AYON Server via code.
From a user perspective, it seems like magic when you hit a button and things start to work automatically!

Let’s shed some light on the AYON API and unlock one of the greatest powers of AYON.

Get Started

Probably, the easiest way is to mimic what happens inside the code.
For this example, We are going to use postman or Thunder Client - VS Code

Both almost have the same UI!

Get Authentication Token

By making a login request

  1. Write a login request ( <your-ayon-server-url>/api/auth/login )
  2. Add name and password
  3. Send!
  4. Find the token.
Thunder Client Postman

Your first API call

Let’s ask AYON for the available projects, List Projects request.

  1. Write a login request ( <your-ayon-server-url>/api/projects )
  2. Add your token
  3. Send!
  4. Find the available projects.

Make API calls from code

There are many libraries to make API calls.
For this guide we are going to use httpx

It’s as simple as

import httpx
ayon_server_url = "<your-ayon-server-url"
username =  "<your-user-name>"
password = "<your-password>"
req = f"{ayon_server_url}/api/auth/login"
response = httpx.post(
    req,
    json={
        "name": username,
        "password": password
    }
)
token = response.json().get("token")
req = f"{self.ayon_server_url}/api/projects"
response = httpx.get(
    req,
    headers={
        "Authorization": f"Bearer {self.token}"
    }
)
projects = response.json().get("projects")
# you can write some code to process the result.
# But, I'll just print the result as it is.
print(projects)

AYON API day-to-day usage

How using AYON API can be useful in my day to day work ?
Well, it can speed up your dev process a lot!
E.g. I use it on daily basis to upload addons Instead of doing it manually!
where I run this in terminal.

python E:\Ynput\ayon-core\server_addon\create_ayon_addons.py --addon houdini 
python E:\Ynput\ayon-recipes\simple_ayon_rest_commands.py

These commands do the following:

  1. Create houdini.#.#.#.zip
  2. Delete the Houdini addon with the same version.
  3. Upload the addon’s zip file.

In simple_ayon_rest_commands.py, I’m using my class SimpleAyonRestCommands. find its definition in the Extended Example Code.

# fetch ayon server credentials
filepath = __file__.replace(".py", ".json")
with open(filepath, "r") as fp:
  data = json.load(fp)
  
username =  data["username"]
password = data["password"]
ayon_server_url = data["ayon_server_url"]
addon_path = data["addon_path"]
my_ayon = SimpleAyonRestCommands(ayon_server_url, 
                                 username,
                                 password)

file_name = os.path.basename(addon_path).rstrip(".zip")
addon_name, addon_version = file_name.split("-", 1)
## Delete and addon by version
print("Delete addon: {}, version: {}".format(addon_name, addon_version))
my_ayon.delete_addon_version(addon_name, addon_version)
  
## Upload Addon from file on disk 
print("Upload addon: {}, version: {}".format(addon_name, addon_version))
res = my_ayon.upload_addon_zip_file(addon_path) 
print (res)

Extended Example Code

You can find an extended example here

References

1 Like