|
| 1 | +# Server Authentication |
| 2 | + |
| 3 | +When configuring webhooks for your assistant, you can authenticate your server endpoints using either a secret token, custom headers, or OAuth2. This ensures that only authorized requests from Vapi are processed by your server. |
| 4 | + |
| 5 | +## Credential Configuration |
| 6 | + |
| 7 | +Credentials can be configured at multiple levels: |
| 8 | + |
| 9 | +1. **Tool Call Level**: Create individual credentials for each tool call |
| 10 | +2. **Assistant Level**: Set credentials directly in the assistant configuration |
| 11 | +3. **Phone Number Level**: Configure credentials for specific phone numbers |
| 12 | +4. **Organization Level**: Manage credentials in the [API Keys page](https://dashboard.vapi.ai/keys) |
| 13 | + |
| 14 | +The order of precedence is: |
| 15 | +1. Tool call-level credentials |
| 16 | +2. Assistant-level credentials |
| 17 | +3. Phone number-level credentials |
| 18 | +4. Organization-level credentials from the API Keys page |
| 19 | + |
| 20 | +## Authentication Methods |
| 21 | + |
| 22 | +### Secret Token Authentication |
| 23 | + |
| 24 | +The simplest way to authenticate webhook requests is using a secret token. Vapi will include this token in the `X-Vapi-Signature` header of each request. |
| 25 | + |
| 26 | +#### Configuration |
| 27 | + |
| 28 | +```json |
| 29 | +{ |
| 30 | + "server": { |
| 31 | + "url": "https://your-server.com/webhook", |
| 32 | + "secret": "your-secret-token" |
| 33 | + } |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +### Custom Headers Authentication |
| 38 | + |
| 39 | +For more complex authentication scenarios, you can configure custom headers that Vapi will include with each webhook request. |
| 40 | + |
| 41 | +This could include short lived JWTs/API Keys passed along via the Authorization header, or any other header that your server checks for. |
| 42 | + |
| 43 | +#### Configuration |
| 44 | + |
| 45 | +```json |
| 46 | +{ |
| 47 | + "server": { |
| 48 | + "url": "https://your-server.com/webhook", |
| 49 | + "headers": { |
| 50 | + "Authorization": "Bearer your-api-key", |
| 51 | + "Custom-Header": "custom-value" |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +### OAuth2 Authentication |
| 58 | + |
| 59 | +For OAuth2-protected webhook endpoints, you can configure OAuth2 credentials that Vapi will use to obtain and refresh access tokens. |
| 60 | + |
| 61 | +#### Configuration |
| 62 | + |
| 63 | +```json |
| 64 | +{ |
| 65 | + "server": { |
| 66 | + "url": "https://your-server.com/webhook" |
| 67 | + }, |
| 68 | + "credentials": { |
| 69 | + "webhook": { |
| 70 | + "type": "oauth2", |
| 71 | + "clientId": "your-client-id", |
| 72 | + "clientSecret": "your-client-secret", |
| 73 | + "tokenUrl": "https://your-server.com/oauth/token", |
| 74 | + "scope": "optional, only needed to specify which scopes to request access for" |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +#### OAuth2 Flow |
| 81 | + |
| 82 | +1. Vapi makes a request to your token endpoint with client credentials |
| 83 | +2. Your server validates the credentials and returns an access token |
| 84 | +3. Vapi includes the access token in the Authorization header for webhook requests |
| 85 | +4. Your server validates the access token before processing the webhook |
| 86 | +5. When the token expires, Vapi automatically requests a new one |
| 87 | + |
| 88 | +#### OAuth2 Token Response Format |
| 89 | + |
| 90 | +Your server should return a JSON response with the following format: |
| 91 | + |
| 92 | +```json |
| 93 | +{ |
| 94 | + "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...", |
| 95 | + "token_type": "Bearer", |
| 96 | + "expires_in": 3600, |
| 97 | + "refresh_token": "tGzv3JOkF0XG5Qx2TlKWIA", // Optional |
| 98 | + "scope": "read write" // Optional, only if scope was requested |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +Example error response: |
| 103 | + |
| 104 | +```json |
| 105 | +{ |
| 106 | + "error": "invalid_client", |
| 107 | + "error_description": "Invalid client credentials" |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +Common error types: |
| 112 | +- `invalid_client`: Invalid client credentials |
| 113 | +- `invalid_grant`: Invalid or expired refresh token |
| 114 | +- `invalid_scope`: Invalid scope requested |
| 115 | +- `unauthorized_client`: Client not authorized for this grant type |
0 commit comments