Using the Valohai APIs is rather straightforward, you’ll need to create an API token to authenticate your requests and then write your code to send & receive requests.
Generate a Valohai API Token
- Go to Valohai.
- Click on the “
Hi, <username>!
” on 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.
In our sample we’ll paste this directly to the file but you should consider saving this token in a configuration file or database for secure storage.
Create a custom virtual environment (optional)
Consider creating a custom virtual environment for Python before continuing.
python3 -m virtualenv .venv
source .venv/bin/activate
Create the script for fetching executions
- You’ll need the ID of a single project to fetch its executions. You can get it from the projects’ setting page or query for it using the Valohai APIs results.
- Create a new file fetchExecutions.py
import requests
import json
import os
# Authenticate yourself with the token.# 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}
# Send a request (with the authentication headers) to fetch all executions in a project
# You can get the project ID for example
resp = requests.get('https://app.valohai.com/api/v0/executions/?project={project_id}', headers=headers)
# To fetch all failed executions you could run
# https://app.valohai.com/api/v0/executions/?project={project_id}&status=error
resp.raise_for_status()
# Print the response you've received back
print('# API Response:\n')
print(json.dumps(resp.json(), indent=4))
Save and run python fetchExecutions.py
and you’ll see a list of executions with their details.