Authentication
The Twin.Actor API supports two authentication methods: JWT Bearer tokens for user-facing applications and API keys for server-to-server integrations.
Authentication Methods
JWT Bearer Token
Best for web and mobile applications where users log in with their credentials. Tokens expire after 7 days.
Authorization</span>: Bearer eyJhbGciOiJIUzI1NiIs...API Key (Coming Soon)
Best for server-to-server integrations and automated systems. Keys can be scoped and rotated.
X-API-Key</span>: sk_live_abc123...Getting a Token
Register a New Account
/auth/registerCreate a new user account
Request Body
| Name | Type | Description |
|---|---|---|
emailrequired | string | Valid email address |
passwordrequired | string | Minimum 8 characters |
name | string | Display name |
curl -X POST https://api.twin.actor/api/v1/auth/register \
-H "Content-Type: application/json" \
-d '{
"email": "developer@example.com",
"password": "secure_password_123",
"name": "Jane Developer"
}'Response:
{
"user": {
"id": "usr_abc123",
"email": "developer@example.com",
"name": "Jane Developer",
"credits": 1000,
"is_verified": false
},
"token": {
"access_token": "eyJhbGciOiJIUzI1NiIs...",
"token_type": "bearer"
},
"message": "Registration successful! Check your email to verify your account."
}Login
/auth/loginAuthenticate with email and password
OAuth2 Form Format
application/x-www-form-urlencoded content type.Request Body (Form)
| Name | Type | Description |
|---|---|---|
usernamerequired | string | Your email address |
passwordrequired | string | Your password |
curl -X POST https://api.twin.actor/api/v1/auth/login \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "username=developer@example.com&password=secure_password_123"Response:
{
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c3JfYWJjMTIzIiwiZW1haWwiOiJkZXZlbG9wZXJAZXhhbXBsZS5jb20iLCJleHAiOjE3MTEwMDAwMDB9.signature",
"token_type": "bearer"
}Using Your Token
Include the token in the Authorization header of every request:
GET</span> /api/v1/persons HTTP/1.1
Host</span>: api.twin.actor
Authorization</span>: Bearer eyJhbGciOiJIUzI1NiIs...curl https://api.twin.actor/api/v1/persons \
-H "Authorization: Bearer YOUR_TOKEN"Token Expiration
JWT tokens expire after 7 days. When a token expires, you'll receive a 401 error:
{
"detail": "Token has expired"
}To continue using the API, simply log in again to get a new token. We recommend:
- Store the token expiration time when you receive it
- Refresh the token before it expires (e.g., after 6 days)
- Handle 401 errors gracefully by re-authenticating
Decoding Token Expiration
"text-violet-400">import base64
"text-violet-400">import json
# Get the payload (second part of the token)
payload = token.split(".")[1]
# Add padding "text-violet-400">and decode
payload += "=" * (4 - len(payload) % 4)
data = json.loads(base64.b64decode(payload))
expires_at = data["exp"] # Unix timestampGet Current User
/auth/meGet the currently authenticated user's profile
curl https://api.twin.actor/api/v1/auth/me \
-H "Authorization: Bearer YOUR_TOKEN"Response:
{
"id": 1,
"email": "developer@example.com",
"name": "Jane Developer",
"credits": 8500,
"is_verified": true,
"created_at": "2024-01-15T10:30:00Z",
"updated_at": "2024-03-20T14:45:00Z"
}Check Credits
/auth/creditsGet current credit balance and pricing information
curl https://api.twin.actor/api/v1/auth/credits \
-H "Authorization: Bearer YOUR_TOKEN"Response:
{
"credits": 8500,
"credits_per_second_video": 10,
"credits_per_photo": 100
}Email Verification
New accounts must verify their email address. A verification link is sent during registration.
Verify Email
/auth/verify-emailVerify email with token from verification link
curl -X POST "https://api.twin.actor/api/v1/auth/verify-email?token=VERIFICATION_TOKEN"Resend Verification
/auth/resend-verificationRequest a new verification email
curl -X POST https://api.twin.actor/api/v1/auth/resend-verification \
-H "Authorization: Bearer YOUR_TOKEN"Password Management
Change Password
/auth/change-passwordChange password for the current user
Request Body
| Name | Type | Description |
|---|---|---|
current_passwordrequired | string | Current password |
new_passwordrequired | string | New password (min 8 characters) |
curl -X POST https://api.twin.actor/api/v1/auth/change-password \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"current_password": "old_password",
"new_password": "new_secure_password"
}'Error Responses
Authentication errors return these status codes:
Example error response:
{
"detail": "Could not validate credentials"
}Best Practices
- Never expose tokens in client-side code - Use a backend proxy for browser applications
- Use environment variables - Store tokens in
TWIN_ACTOR_TOKENor similar - Handle expiration gracefully - Implement automatic re-authentication when tokens expire
- Use HTTPS only - Never send tokens over unencrypted connections
- Rotate tokens regularly - Re-authenticate periodically for long-running services