Page options
Biocompile API quick start: a first call with curl and Python
Last updated
The Biocompile API quick start ends with a short script that lists your organizations and their research projects. You create a personal access token in the app, exchange it for a short-lived access token, and make your first calls with curl. The same calls in Python follow the steps.
You need a Biocompile account and a terminal with curl and jq installed.
Make your first API call
Every call uses an access token that lasts 10 minutes, exchanged from the personal access token you create in the first step:
-
Open Settings → API tokens and choose Create token. Result: the New personal access token form opens.
-
Enter a Name, choose your organization under Organization, tick the
projects:readscope and create the token. Result: the token is shown once, under “Copy it now. You won’t see this again.” -
Copy the token and keep it in an environment variable, never in your code:
export BIOCOMPILE_PAT='paste-your-token-here' -
Exchange it for an access token at the token endpoint:
ACCESS_TOKEN=$(curl -s https://app.biocompile.com/api/v1/oauth/token \ -d grant_type=urn:ietf:params:oauth:grant-type:token-exchange \ -d subject_token_type=urn:biocompile:params:oauth:token-type:pat \ --data-urlencode "subject_token=$BIOCOMPILE_PAT" | jq -r .access_token)Result:
ACCESS_TOKENholds a token that works for 600 seconds. -
List the organizations you belong to:
curl -s https://app.biocompile.com/api/v1/organizations/ \ -H "Authorization: Bearer $ACCESS_TOKEN"Result: the answer is
{"data": [...], "count": n}, one entry per organization. -
List the research projects of the first organization:
ORG_ID=$(curl -s https://app.biocompile.com/api/v1/organizations/ \ -H "Authorization: Bearer $ACCESS_TOKEN" | jq -r '.data[0]._id') curl -s "https://app.biocompile.com/api/v1/organizations/$ORG_ID/research-projects/?limit=20" \ -H "Authorization: Bearer $ACCESS_TOKEN"Result: you see the projects you can open in the app, and no others.
After 10 minutes the API answers 401. Exchange the personal access token again, ideally a
minute before the expires_in time of the exchange runs out.
Make the same calls in Python
Any HTTP client works with the API. With httpx, the exchange and the first call look like
this:
import os
import httpx
BASE = "https://app.biocompile.com"
grant = httpx.post(
f"{BASE}/api/v1/oauth/token",
data={
"grant_type": "urn:ietf:params:oauth:grant-type:token-exchange",
"subject_token_type": "urn:biocompile:params:oauth:token-type:pat",
"subject_token": os.environ["BIOCOMPILE_PAT"],
},
).raise_for_status().json()
headers = {"Authorization": f"Bearer {grant['access_token']}"}
organizations = httpx.get(f"{BASE}/api/v1/organizations/", headers=headers)
organizations.raise_for_status()
for organization in organizations.json()["data"]:
print(organization["_id"], organization["name"])
A script that runs longer than 10 minutes exchanges the token again before it expires.
Next steps
Was this page helpful?
Something wrong or missing?