Authentication
Overview
Section titled “Overview”The SiteAssist API uses two authentication methods depending on the endpoint and use case:
- API Key Authentication - For initializing sessions and public operations
- Bearer Token Authentication - For authenticated requests after session initialization
API Key Authentication
Section titled “API Key Authentication”API keys are used to identify your project and initialize visitor sessions. They are passed directly in the request body or as query parameters.
Using Publishable Keys
Section titled “Using Publishable Keys”Publishable keys (pk_*) are used in client-side applications to initialize visitor sessions:
// Initialize a visitor session with your publishable keyconst response = await fetch('https://api.siteassist.io/v2/identity/init', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ apiKey: 'pk_your_publishable_key_here', saVid: 'unique-device-id-123', }),});Using Private Keys
Section titled “Using Private Keys”Private keys (sk_*) provide full access to the API and should only be used server-side. They are typically used as Bearer tokens:
// Using private key as Bearer token (server-side only)const response = await fetch('https://api.siteassist.io/v2/chat/completions', { method: 'POST', headers: { 'Authorization': 'Bearer sk_your_private_key_here', 'Content-Type': 'application/json', }, body: JSON.stringify({ messages: [ { role: 'user', content: 'Hi' } ] })});Bearer Token Authentication
Section titled “Bearer Token Authentication”After initializing a visitor session, you receive a short-lived session token that must be used for subsequent authenticated requests.
Session Token Lifecycle
Section titled “Session Token Lifecycle”- Initialize: Call
/identity/initwith your publishable API key - Receive: Get a
sessionTokenin the response - Use: Include the token in the
Authorizationheader for subsequent requests - Expire: Session tokens are short-lived and expire after a period of inactivity
Example: Complete Authentication Flow
Section titled “Example: Complete Authentication Flow”// Step 1: Initialize visitor sessionconst initResponse = await fetch('https://api.siteassist.io/v2/identity/init', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ apiKey: 'pk_your_publishable_key_here', saVid: 'device-abc-123', }),});
const { sessionToken, visitor } = await initResponse.json();
// Step 2: Use session token for authenticated requestsconst conversationResponse = await fetch('https://api.siteassist.io/v2/conversations', { method: 'POST', headers: { 'Authorization': `Bearer ${sessionToken}`, 'Content-Type': 'application/json', }, body: JSON.stringify({ context: { pageUrl: 'https://example.com/support', }, }),});
const conversation = await conversationResponse.json();
// Step 3: Continue using the same session token// for all requests in this sessionconsole.log('Visitor ID:', visitor.id);console.log('Conversation ID:', conversation.id);Authentication by Endpoint
Section titled “Authentication by Endpoint”Different endpoints use different authentication methods:
| Endpoint | Authentication Method | Key Type |
|---|---|---|
POST /identity/init |
API Key (in body) | Publishable |
GET /identity/me |
Bearer Token | Session Token |
GET /projects/current |
API Key (query param) | Publishable |
POST /conversations |
Bearer Token | Session Token |
GET /conversations |
Bearer Token | Session Token |
POST /conversations/{id}/messages |
Bearer Token | Session Token |
GET /projects/{id}/qnas |
No auth required | - |
GET /search |
API Key (query param) | Publishable |
POST /chat/completions |
Bearer Token | Private |
Error Responses
Section titled “Error Responses”When authentication fails, the API returns appropriate error responses:
401 Unauthorized
Section titled “401 Unauthorized”The authentication token is missing or invalid:
{ "error": { "message": "Authentication required", "code": "UNAUTHORIZED" }}Common causes:
- Missing
Authorizationheader - Expired session token
- Invalid or revoked API key
- Malformed Bearer token
403 Forbidden
Section titled “403 Forbidden”The authenticated user doesn’t have permission to access the resource:
{ "error": { "message": "Access denied to this conversation", "code": "FORBIDDEN" }}Common causes:
- Trying to access another visitor’s conversation
- Using a publishable key for admin operations
- Domain restrictions on publishable key
Best Practices
Section titled “Best Practices”Security
Section titled “Security”- Store Keys Securely: Use environment variables, never hardcode keys
- Rotate Keys Regularly: Create new keys and revoke old ones periodically
- Use HTTPS Only: Never send API keys over unencrypted connections
- Implement Key Rotation: Have a process to update keys without downtime
# Good: Store in environment variablesSITEASSIST_PUBLISHABLE_KEY=pk_abc123...SITEASSIST_SECRET_KEY=sk_xyz789...
# Never commit these to version control!Need Help?
Section titled “Need Help?”We’re here to help you succeed! Whether you have questions, need assistance with setup, or want to discuss advanced use cases, our team is ready to provide personalized support.
Contact us: support@siteassist.io
We’d love to hear about your use case and help you get the most out of SiteAssist!