Quick Examples
Get Service User Info
Get Service User Info
Copy
Ask AI
import os
import requests
TOKEN = os.getenv("DEVIN_SERVICE_USER_TOKEN")
BASE_URL = "https://api.devin.ai/v3beta1"
response = requests.get(
f"{BASE_URL}/enterprise/self",
headers={"Authorization": f"Bearer {TOKEN}"}
)
self_info = response.json()
print(f"Service User: {self_info['name']}")
print(f"Role: {self_info['role']}")
List Organizations
List Organizations
Copy
Ask AI
import os
import requests
TOKEN = os.getenv("DEVIN_SERVICE_USER_TOKEN")
BASE_URL = "https://api.devin.ai/v3beta1"
response = requests.get(
f"{BASE_URL}/enterprise/organizations",
headers={"Authorization": f"Bearer {TOKEN}"}
)
orgs = response.json()
for org in orgs["organizations"]:
print(f"{org['name']} ({org['id']})")
Create an Organization
Create an Organization
Copy
Ask AI
import os
import requests
TOKEN = os.getenv("DEVIN_SERVICE_USER_TOKEN")
BASE_URL = "https://api.devin.ai/v3beta1"
response = requests.post(
f"{BASE_URL}/enterprise/organizations",
headers={
"Authorization": f"Bearer {TOKEN}",
"Content-Type": "application/json"
},
json={
"name": "Data Science Team",
"acu_limit": 500
}
)
org = response.json()
print(f"Created organization: {org['id']}")
Create a Session
Create a Session
Copy
Ask AI
import os
import requests
TOKEN = os.getenv("DEVIN_SERVICE_USER_TOKEN")
BASE_URL = "https://api.devin.ai/v3beta1"
org_id = "your_org_id"
response = requests.post(
f"{BASE_URL}/organizations/{org_id}/sessions",
headers={
"Authorization": f"Bearer {TOKEN}",
"Content-Type": "application/json"
},
json={
"prompt": "Create a Python script that analyzes CSV data"
}
)
session = response.json()
print(f"Session created: {session['session_id']}")
print(f"URL: {session['url']}")
Get Consumption by Organization
Get Consumption by Organization
Copy
Ask AI
import os
import requests
TOKEN = os.getenv("DEVIN_SERVICE_USER_TOKEN")
BASE_URL = "https://api.devin.ai/v3beta1"
response = requests.get(
f"{BASE_URL}/enterprise/consumption/daily/organizations",
headers={"Authorization": f"Bearer {TOKEN}"},
params={"start_date": "2024-01-01", "end_date": "2024-01-31"}
)
consumption = response.json()
for entry in consumption["data"]:
print(f"{entry['organization_name']}: {entry['acus_used']} ACUs")
Create a Service User
Create a Service User
Copy
Ask AI
import os
import requests
TOKEN = os.getenv("DEVIN_SERVICE_USER_TOKEN")
BASE_URL = "https://api.devin.ai/v3beta1"
org_id = "your_org_id"
response = requests.post(
f"{BASE_URL}/organizations/{org_id}/members/service-users",
headers={
"Authorization": f"Bearer {TOKEN}",
"Content-Type": "application/json"
},
json={
"name": "CI/CD Bot",
"role": "developer"
}
)
service_user = response.json()
print(f"Created service user: {service_user['id']}")
print(f"API Key: {service_user['api_key']}") # Only shown once!
Get Audit Logs
Get Audit Logs
Copy
Ask AI
import os
import requests
from datetime import datetime, timedelta, timezone
TOKEN = os.getenv("DEVIN_SERVICE_USER_TOKEN")
BASE_URL = "https://api.devin.ai/v3beta1"
# Last 7 days in UTC, as Unix timestamps (seconds)
now = datetime.now(timezone.utc)
seven_days_ago = now - timedelta(days=7)
params = {
"time_after": int(seven_days_ago.timestamp()),
"time_before": int(now.timestamp()),
"first": 100, # page size (max 200)
}
response = requests.get(
f"{BASE_URL}/enterprise/audit-logs",
headers={"Authorization": f"Bearer {TOKEN}"},
params=params,
)
response.raise_for_status()
data = response.json()
for log in data["items"]:
ts = datetime.fromtimestamp(log["created_at"], tz=timezone.utc).isoformat()
actor_id = log.get("service_user_id") or log.get("user_id") or "system"
print(f"{ts} - {log['action']} (org={log.get('org_id')}, actor={actor_id})")
# To fetch additional pages, use end_cursor when has_next_page is True:
# if data["has_next_page"]:
# params["after"] = data["end_cursor"]
# # Make another request with the updated params
Multi-Organization Workflow
Multi-Organization Workflow
Automate across multiple organizations:
Copy
Ask AI
import os
import requests
TOKEN = os.getenv("DEVIN_SERVICE_USER_TOKEN")
BASE_URL = "https://api.devin.ai/v3beta1"
# Get all organizations
orgs_response = requests.get(
f"{BASE_URL}/enterprise/organizations",
headers={"Authorization": f"Bearer {TOKEN}"}
)
organizations = orgs_response.json()["organizations"]
# Create a session in each organization
for org in organizations:
session_response = requests.post(
f"{BASE_URL}/organizations/{org['id']}/sessions",
headers={
"Authorization": f"Bearer {TOKEN}",
"Content-Type": "application/json"
},
json={
"prompt": f"Run daily health check for {org['name']}"
}
)
if session_response.ok:
session = session_response.json()
print(f"Created session for {org['name']}: {session['session_id']}")
else:
print(f"Failed to create session for {org['name']}")
Error Handling
Error Handling
Copy
Ask AI
import os
import requests
TOKEN = os.getenv("DEVIN_SERVICE_USER_TOKEN")
BASE_URL = "https://api.devin.ai/v3beta1"
try:
response = requests.get(
f"{BASE_URL}/enterprise/organizations",
headers={"Authorization": f"Bearer {TOKEN}"}
)
response.raise_for_status()
data = response.json()
except requests.exceptions.HTTPError as e:
if e.response.status_code == 401:
print("Invalid or expired service user token")
elif e.response.status_code == 403:
print("Service user lacks required role/permission")
elif e.response.status_code == 429:
print("Rate limit exceeded - wait and retry")
else:
print(f"API error: {e}")
Support
Need Help?
For questions about the API or to report issues, email [email protected]
