The Valohai API docs available in your Valohai environment. For most users that’ll be at:
For users with a self-hosted Valohai installation the address will be under their installation, e.g. https://youraddress/api/v0/
Get Started
You can use any programming language you want to make HTTP requests.
In this tutorial, we will be using Python 3 (version 3.8+ recommended). Installing Python and pip is outside the scope of this tutorial, but a good place for information is docs.python-guide.org.
To validate your environment, open a terminal and run:
python --version
We also need to install the requests library to make HTTP requests:
pipx install requests
Get an Authentication Token
- Go to Valohai.
- Click on the “
Hi, <username>!
” in the top-right corner. - Go to My Profile -> Authentication.
- Click on Manage Tokens and scroll to the bottom of the page to generate a new token.
- Make a note of the token that pops up on the top of your page. It will be shown only once.
Keep your token safe
The generated token is personal, and it gives you access to Valohai features through your account. You might want to save the token in a local environment variable, a configuration file, or a database Don’t include it directly in your version-controlled files.
Make an API Request
Let’s start running a Valohai API call.
Environment variable
The example below assumes the token is available through an environment variable.
You can set an environment variable on Linux/Mac with:
export VH_API_TOKEN=yourtokenvalue
or on Windows with:
set VH_API_TOKEN=yourtokenvalue
import requests
import json
import os
# Get the Valohai API token you created earlier.
# Here it has been stored in a local environment variable.
# Remember to follow your organization's security standards when handling the token.
auth_token = os.environ['VH_API_TOKEN']
headers = {'Authorization': 'Token %s' % auth_token}
resp = requests.get('https://app.valohai.com/api/v0/projects/', headers=headers)
resp.raise_for_status()
print(json.dumps(resp.json(), indent=4))
Save the code as test.py
and run it locally with python test.py
.
You should get a response like the one below:
{
"count": 1,
"next": null,
"previous": null,
"results": [
{
"id": "015f25c1-0101-1010-fefe-a0a0a0a0a0a0",
"name": "my-project",
"description": "",
"owner": {
"id": 1337,
"username": "myusername"
},
"ctime": "2017-10-16T15:16:19.230872Z",
"mtime": "2017-10-16T15:16:19.230895Z",
"url": "https://app.valohai.com/api/v0/projects/015f25c1-0101-1010-fefe-a0a0a0a0a0a0/",
"urls": {
"display": "https://app.valohai.com/p/myusername/my-project/",
"display_repository": "https://app.valohai.com/p/myusername/my-project/settings/repository/"
},
"execution_count": 0,
"last_execution_ctime": null
}
]
}