Python Quickstart
From zero to causal inference in 10 minutes.
Prerequisites
- Python 3.10 or later
pip install requests- A Galen API key — get one free
Set Up Authentication
All requests authenticate with the X-API-Key header. Set up a shared session to keep your code clean.
import requests
API_KEY = "gk_live_your_key_here" # Get yours at developer.usegalen.com/auth
BASE = "https://research.usegalen.com/api/v1"
headers = {"X-API-Key": API_KEY}Look Up an Entity
Fetch an entity by name to get its type, evidence tier, and relationship count.
# Get the EGFR gene profile
entity = requests.get(f"{BASE}/entities/EGFR", headers=headers).json()
print(f"Name: {entity['name']}")
print(f"Type: {entity['entity_type']}")
print(f"Evidence tier: {entity['evidence_tier']}")
print(f"Relationships: {entity['relationship_count']}")Traverse Relationships
Explore how an entity connects to drugs, pathways, and other genes. Filter by relationship type and limit the results.
# Get EGFR's treatment relationships
rels = requests.get(
f"{BASE}/entities/EGFR/relationships",
headers=headers,
params={"relationship_type": "treated_by", "limit": 5}
).json()
for rel in rels["relationships"]:
print(f" {rel['target']}: {rel['relationship_type']} (L{rel['pch_layer']})")Query a Database
Access integrated databases directly. Here we look up a compound in ChEMBL to get its molecular properties and known targets.
# Look up erlotinib in ChEMBL
compound = requests.get(
f"{BASE}/chembl/compound/erlotinib",
headers=headers
).json()
print(f"Molecular weight: {compound['molecular_weight']}")
print(f"Targets: {len(compound['targets'])}")Simulate a Causal Intervention
Use the causal inference engine to simulate what happens when you inhibit or activate a target. The response traces downstream effects through the knowledge graph with confidence scores.
# What happens if we inhibit EGFR?
result = requests.post(
f"{BASE}/causal/intervention",
headers=headers,
json={"target": "EGFR", "intervention_type": "inhibit"}
).json()
print(f"Downstream effects: {len(result['downstream_effects'])}")
for effect in result["downstream_effects"][:5]:
print(f" {effect['entity']}: {effect['effect_type']} ({effect['confidence']:.2f})")Error Handling
The API returns standard HTTP status codes. Handle the common cases to make your integration robust.
response = requests.get(f"{BASE}/entities/nonexistent", headers=headers)
if response.status_code == 404:
error = response.json()
print(f"Error: {error['detail']}")
elif response.status_code == 429:
retry_after = response.headers.get("Retry-After", 60)
print(f"Rate limited. Retry after {retry_after}s")
elif response.status_code != 200:
print(f"Unexpected error: {response.status_code}")