API walkthrough
This guide walks through how to accomplish common Braintrust use cases with the API. These examples are written in Python, for legibility, but the API itself is language-agnostic. To learn more about the API, see the full API spec.
Running an experiment
import os
from uuid import uuid4
import requests
API_URL = "https://api.braintrustdata.com/v1"
headers = {"Authorization": "Bearer " + os.environ["BRAINTRUST_API_KEY"]}
if __name__ == "__main__":
# Create a project, if it does not already exist
project = requests.post(f"{API_URL}/project", headers=headers, json={"name": "rest_test"}).json()
print(project)
# Create an experiment. This should always be new
experiment = requests.post(
f"{API_URL}/experiment", headers=headers, json={"name": "rest_test", "project_id": project["id"]}
).json()
print(experiment)
# Log some stuff
for i in range(10):
resp = requests.post(
f"{API_URL}/experiment/{experiment['id']}/insert",
headers=headers,
json={"events": [{"id": uuid4().hex, "input": 1, "output": 2, "scores": {"accuracy": 0.5}}]},
)
if not resp.ok:
raise Exception(f"Error: {resp.status_code} {resp.text}: {resp.content}")