Authentication
Cohera supports multiple authentication methods to fit different use cases. Choose the method that best fits your application.
Authentication Methods
Section titled “Authentication Methods”| Method | Best For | Security Level |
|---|---|---|
| API Keys | Server-to-server, scripts, CLI | High |
| OAuth2 | User-facing applications | Highest |
| JWT Tokens | Microservices, short-lived access | High |
API Keys
Section titled “API Keys”API keys are the simplest way to authenticate with Cohera. They’re ideal for server-side applications, scripts, and development.
Creating an API Key
Section titled “Creating an API Key”- Log in to your Cohera Dashboard
- Go to Settings > API Keys
- Click Create New Key
- Select the appropriate scopes
- Set an expiration date (optional but recommended)
- Copy and securely store the key
Using API Keys
Section titled “Using API Keys”from cohera import Cohera
# Via environment variable (recommended)import osos.environ["COHERA_API_KEY"] = "ck_live_..."client = Cohera()
# Via constructorclient = Cohera(api_key="ck_live_...")import { Cohera } from '@cohera/sdk';
// Via environment variable (recommended)const client = new Cohera();// Reads from COHERA_API_KEY
// Via constructorconst client = new Cohera({ apiKey: 'ck_live_...' });curl -X GET "https://api.cohera.io/v1/certificates" \ -H "Authorization: Bearer ck_live_..."API Key Scopes
Section titled “API Key Scopes”Control what your API key can access:
| Scope | Description |
|---|---|
read:certificates | Read certificate data |
write:certificates | Create and update certificates |
read:suppliers | Read supplier data |
write:suppliers | Create and update suppliers |
read:products | Read product data |
write:products | Create and update products |
admin | Full access to all resources |
OAuth2
Section titled “OAuth2”OAuth2 is recommended for applications that act on behalf of users. It provides the highest level of security and allows users to control their data access.
OAuth2 Flow
Section titled “OAuth2 Flow”┌──────────┐ ┌──────────┐ ┌──────────┐│ User │────>│ Your App │────>│ Cohera │└──────────┘ └──────────┘ └──────────┘ │ │ │ │ 1. Login │ │ │───────────────>│ │ │ │ 2. Redirect │ │ │───────────────>│ │ │ │ │ 3. Authorize │<───────────────│ │<───────────────│ │ │ │ │ │ │ 4. Token │ │ │<───────────────│ │ │ │ │ 5. Access │ │ │<───────────────│ │└──────────────────────────────────────┘Setting Up OAuth2
Section titled “Setting Up OAuth2”- Register your application in the Developer Console
- Configure your redirect URIs
- Note your Client ID and Client Secret
- Implement the authorization flow
Implementation
Section titled “Implementation”from cohera import Coherafrom cohera.auth import OAuth2
# Step 1: Get authorization URLoauth = OAuth2( client_id="your-client-id", client_secret="your-client-secret", redirect_uri="https://yourapp.com/callback")
auth_url = oauth.get_authorization_url( scopes=["read:certificates", "read:suppliers"])# Redirect user to auth_url
# Step 2: Handle callbackcode = request.args.get("code") # From callbacktokens = oauth.exchange_code(code)
# Step 3: Use the access tokenclient = Cohera(access_token=tokens.access_token)certificates = client.certificates.list()
# Step 4: Refresh when neededif tokens.is_expired: tokens = oauth.refresh_token(tokens.refresh_token)import { Cohera, OAuth2 } from '@cohera/sdk';
// Step 1: Get authorization URLconst oauth = new OAuth2({ clientId: 'your-client-id', clientSecret: 'your-client-secret', redirectUri: 'https://yourapp.com/callback'});
const authUrl = oauth.getAuthorizationUrl({ scopes: ['read:certificates', 'read:suppliers']});// Redirect user to authUrl
// Step 2: Handle callbackconst code = req.query.code; // From callbackconst tokens = await oauth.exchangeCode(code);
// Step 3: Use the access tokenconst client = new Cohera({ accessToken: tokens.accessToken });const certificates = await client.certificates.list();
// Step 4: Refresh when neededif (tokens.isExpired) { const newTokens = await oauth.refreshToken(tokens.refreshToken);}JWT Tokens
Section titled “JWT Tokens”JWT tokens are used for service-to-service authentication and short-lived access. They’re signed tokens that contain claims about the authenticated entity.
Generating a JWT
Section titled “Generating a JWT”from cohera import Coherafrom cohera.auth import ServiceAccount
# Load service account credentialssa = ServiceAccount.from_file("service-account.json")
# Generate a JWTtoken = sa.generate_token( scopes=["read:certificates"], expires_in=3600 # 1 hour)
# Use the tokenclient = Cohera(access_token=token)import { Cohera, ServiceAccount } from '@cohera/sdk';import { readFileSync } from 'fs';
// Load service account credentialsconst sa = ServiceAccount.fromFile('service-account.json');
// Generate a JWTconst token = await sa.generateToken({ scopes: ['read:certificates'], expiresIn: 3600 // 1 hour});
// Use the tokenconst client = new Cohera({ accessToken: token });JWT Structure
Section titled “JWT Structure”Cohera JWTs contain the following claims:
{ "iss": "cohera.io", "sub": "sa_abc123", "aud": "https://api.cohera.io", "exp": 1699999999, "iat": 1699996399, "scopes": ["read:certificates", "read:suppliers"], "org_id": "org_xyz789"}Security Best Practices
Section titled “Security Best Practices”- Never commit credentials - Use environment variables or secret managers
- Rotate keys regularly - Set expiration dates and rotate keys periodically
- Use least privilege - Only request scopes your application needs
- Monitor usage - Review API key usage in the dashboard
- Revoke unused keys - Delete keys that are no longer needed
Environment Variables
Section titled “Environment Variables”# .env file (never commit this!)COHERA_API_KEY=ck_live_...COHERA_CLIENT_ID=your-client-idCOHERA_CLIENT_SECRET=your-client-secretSecret Managers
Section titled “Secret Managers”We recommend using a secret manager in production:
- AWS Secrets Manager
- Google Secret Manager
- HashiCorp Vault
- Azure Key Vault
Rate Limiting
Section titled “Rate Limiting”All authentication methods are subject to rate limits:
| Tier | Requests/min | Requests/day |
|---|---|---|
| Free | 60 | 1,000 |
| Pro | 600 | 50,000 |
| Enterprise | Custom | Custom |
Rate limit headers are included in every response:
X-RateLimit-Limit: 600X-RateLimit-Remaining: 599X-RateLimit-Reset: 1699999999Next Steps
Section titled “Next Steps”- Quickstart - Make your first API call
- Python SDK - Full Python SDK documentation
- API Reference - Complete API documentation