Overview
OptiFlow uses Laravel Sanctum for API token authentication. This provides a simple, secure way to authenticate API requests without the complexity of OAuth.All API endpoints require authentication except for public endpoints like health checks. Unauthenticated requests will receive a
401 Unauthorized response.Authentication Flow
- Obtain an API token from your account settings or via the token generation endpoint
- Include the token in the
Authorizationheader of every request - Workspace context is automatically set based on your user’s current workspace
Obtaining API Tokens
Via Dashboard
The simplest way to obtain an API token:- Log in to your OptiFlow account
- Navigate to Settings > API Tokens
- Click Create New Token
- Give your token a descriptive name
- Copy the token immediately (it won’t be shown again)
Via API (Programmatic)
You can also generate tokens programmatically after authenticating:Including Tokens in Requests
Include your API token in theAuthorization header using the Bearer scheme:
Example Requests
Token Permissions and Scopes
API tokens inherit the permissions of the user who created them. OptiFlow uses Laravel’s Spatie Permission package for fine-grained access control.Permission Examples
| Permission | Description |
|---|---|
invoices:view | View invoices |
invoices:create | Create new invoices |
invoices:edit | Update existing invoices |
invoices:delete | Delete invoices |
products:view | View products |
products:create | Create products |
contacts:view | View contacts |
view_all_locations | Access resources across all workspaces |
API requests will fail with
403 Forbidden if the authenticated user lacks the required permission for the requested operation.Workspace Context
Many API operations require a workspace context. OptiFlow automatically uses the authenticated user’s current workspace, but you can specify a different workspace for multi-workspace operations.Setting Workspace Context
Include theX-Workspace-ID header to specify which workspace the request should operate in:
Default Workspace Behavior
If noX-Workspace-ID is provided:
- The system uses your user’s
current_workspace_id - If no current workspace is set, the first workspace you belong to is used
- If you don’t belong to any workspace, workspace-scoped requests will fail
Token Management
Revoking Tokens
Revoke a token when it’s no longer needed:Token Expiration
By default, Sanctum tokens do not expire. For enhanced security, you can configure token expiration inconfig/sanctum.php:
Multi-Tenant Authentication
Tenant Isolation
OptiFlow uses thestancl/tenancy package:
- Each tenant has its own database
- Users, tokens, and resources are completely isolated
- The tenant is identified by the subdomain in the request URL
- Cross-tenant access is not possible, even with valid credentials
Example
Security Best Practices
Store tokens securely
Store tokens securely
- Use environment variables or secure secret management
- Never hardcode tokens in your application
- Don’t commit tokens to version control
- Use different tokens for different environments (dev, staging, production)
Use HTTPS only
Use HTTPS only
- Always use HTTPS in production
- Never send tokens over unencrypted HTTP
- OptiFlow automatically redirects HTTP to HTTPS
Rotate tokens regularly
Rotate tokens regularly
- Generate new tokens periodically
- Revoke old tokens after rotation
- Use descriptive token names to track usage
Implement proper error handling
Implement proper error handling
- Handle 401 responses by refreshing tokens
- Log authentication failures for security monitoring
- Don’t expose token values in error messages or logs
Error Responses
401 Unauthorized
Returned when no token is provided or the token is invalid:403 Forbidden
Returned when the authenticated user lacks required permissions:419 Page Expired
Session-based authentication expired (typically for web-based API access):Testing Authentication
Test your authentication setup with a simple request:Next Steps
Invoices API
Create and manage invoices
Products API
Manage your product catalog
Contacts API
Work with customers and suppliers
Workspaces API
Manage workspace access