scientific-skills/benchling-integration/references/authentication.md
Benchling supports three authentication methods, each suited for different use cases.
Best for: Personal scripts, prototyping, single-user integrations
How it works:
Obtaining an API Key:
Python SDK Usage:
from benchling_sdk.benchling import Benchling
from benchling_sdk.auth.api_key_auth import ApiKeyAuth
benchling = Benchling(
url="https://your-tenant.benchling.com",
auth_method=ApiKeyAuth("your_api_key_here")
)
Direct HTTP Usage:
curl -X GET \
https://your-tenant.benchling.com/api/v2/dna-sequences \
-u "your_api_key_here:"
Note the colon after the API key with no password.
Environment Variable Pattern:
import os
from benchling_sdk.benchling import Benchling
from benchling_sdk.auth.api_key_auth import ApiKeyAuth
api_key = os.environ.get("BENCHLING_API_KEY")
tenant_url = os.environ.get("BENCHLING_TENANT_URL")
benchling = Benchling(
url=tenant_url,
auth_method=ApiKeyAuth(api_key)
)
Best for: Multi-user applications, service accounts, production integrations
How it works:
Registering an App:
Python SDK Usage:
from benchling_sdk.benchling import Benchling
from benchling_sdk.auth.client_credentials_oauth2 import ClientCredentialsOAuth2
auth_method = ClientCredentialsOAuth2(
client_id="your_client_id",
client_secret="your_client_secret"
)
benchling = Benchling(
url="https://your-tenant.benchling.com",
auth_method=auth_method
)
The SDK automatically handles token refresh.
Direct HTTP Token Flow:
# Get access token
curl -X POST \
https://your-tenant.benchling.com/api/v2/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=client_credentials" \
-d "client_id=your_client_id" \
-d "client_secret=your_client_secret"
# Response:
# {
# "access_token": "token_here",
# "token_type": "Bearer",
# "expires_in": 3600
# }
# Use access token
curl -X GET \
https://your-tenant.benchling.com/api/v2/dna-sequences \
-H "Authorization: Bearer access_token_here"
Best for: Enterprise integrations with existing identity providers, SSO scenarios
How it works:
Requirements:
Identity Provider Configuration:
email claimPython Usage:
# Assuming you have an ID token from your IdP
from benchling_sdk.benchling import Benchling
from benchling_sdk.auth.oidc_auth import OidcAuth
auth_method = OidcAuth(id_token="id_token_from_idp")
benchling = Benchling(
url="https://your-tenant.benchling.com",
auth_method=auth_method
)
Direct HTTP Usage:
curl -X GET \
https://your-tenant.benchling.com/api/v2/dna-sequences \
-H "Authorization: Bearer id_token_here"
DO:
DON'T:
Example with Environment Variables:
import os
from dotenv import load_dotenv # python-dotenv package
# Load from .env file (add .env to .gitignore!)
load_dotenv()
api_key = os.environ["BENCHLING_API_KEY"]
tenant = os.environ["BENCHLING_TENANT_URL"]
API Key Rotation:
App Secret Rotation:
Best Practice: Rotate credentials regularly (e.g., every 90 days) and immediately if compromised.
Principle of Least Privilege:
App Permissions: Apps require explicit access grants to:
Configure these in the Developer Console when setting up your app.
User Permissions: API access mirrors UI permissions:
HTTPS Only: All Benchling API requests must use HTTPS. HTTP requests will be rejected.
IP Allowlisting (Enterprise): Some enterprise accounts can restrict API access to specific IP ranges. Contact Benchling support to configure.
Rate Limiting: Benchling implements rate limiting to prevent abuse:
Tracking API Usage:
Best Practice for Apps: Use OAuth instead of API keys when multiple users interact through your app. This ensures proper audit attribution to the actual user, not just the app.
401 Unauthorized:
Solution:
Authorization: Bearer <token>403 Forbidden:
Solution:
429 Too Many Requests:
Solution:
Quick Test with curl:
# Test API key
curl -X GET \
https://your-tenant.benchling.com/api/v2/users/me \
-u "your_api_key:" \
-v
# Test OAuth token
curl -X GET \
https://your-tenant.benchling.com/api/v2/users/me \
-H "Authorization: Bearer your_token" \
-v
The /users/me endpoint returns the authenticated user's information and is useful for verifying credentials.
Python SDK Test:
from benchling_sdk.benchling import Benchling
from benchling_sdk.auth.api_key_auth import ApiKeyAuth
try:
benchling = Benchling(
url="https://your-tenant.benchling.com",
auth_method=ApiKeyAuth("your_api_key")
)
# Test authentication
user = benchling.users.get_me()
print(f"Authenticated as: {user.name} ({user.email})")
except Exception as e:
print(f"Authentication failed: {e}")
If working with multiple Benchling tenants:
# Configuration for multiple tenants
tenants = {
"production": {
"url": "https://prod.benchling.com",
"api_key": os.environ["PROD_API_KEY"]
},
"staging": {
"url": "https://staging.benchling.com",
"api_key": os.environ["STAGING_API_KEY"]
}
}
# Initialize clients
clients = {}
for name, config in tenants.items():
clients[name] = Benchling(
url=config["url"],
auth_method=ApiKeyAuth(config["api_key"])
)
# Use specific client
prod_sequences = clients["production"].dna_sequences.list()
For environments with self-signed certificates or corporate proxies:
import httpx
from benchling_sdk.benchling import Benchling
from benchling_sdk.auth.api_key_auth import ApiKeyAuth
# Custom httpx client with certificate verification
custom_client = httpx.Client(
verify="/path/to/custom/ca-bundle.crt",
timeout=30.0
)
benchling = Benchling(
url="https://your-tenant.benchling.com",
auth_method=ApiKeyAuth("your_api_key"),
http_client=custom_client
)