|
| 1 | +# Vapi PHP Library |
| 2 | + |
| 3 | +[](https://buildwithfern.com?utm_source=github&utm_medium=github&utm_campaign=readme&utm_source=https%3A%2F%2Fgithub.com%2FVapiAI%2Fserver-sdk-php) |
| 4 | +[](https://packagist.org/packages/vapi/vapi) |
| 5 | + |
| 6 | +The Vapi PHP library provides convenient access to the Vapi APIs from PHP. |
| 7 | + |
| 8 | +## Table of Contents |
| 9 | + |
| 10 | +- [Requirements](#requirements) |
| 11 | +- [Installation](#installation) |
| 12 | +- [Usage](#usage) |
| 13 | +- [Environments](#environments) |
| 14 | +- [Exception Handling](#exception-handling) |
| 15 | +- [Advanced](#advanced) |
| 16 | + - [Custom Client](#custom-client) |
| 17 | + - [Retries](#retries) |
| 18 | + - [Timeouts](#timeouts) |
| 19 | +- [Contributing](#contributing) |
| 20 | + |
| 21 | +## Requirements |
| 22 | + |
| 23 | +This SDK requires PHP ^8.1. |
| 24 | + |
| 25 | +## Installation |
| 26 | + |
| 27 | +```sh |
| 28 | +composer require vapi/vapi |
| 29 | +``` |
| 30 | + |
| 31 | +## Usage |
| 32 | + |
| 33 | +Instantiate and use the client with the following: |
| 34 | + |
| 35 | +```php |
| 36 | +<?php |
| 37 | + |
| 38 | +namespace Example; |
| 39 | + |
| 40 | +use Vapi\VapiClient; |
| 41 | +use Vapi\Types\CreateAssistantDto; |
| 42 | + |
| 43 | +$client = new VapiClient( |
| 44 | + token: '<token>', |
| 45 | +); |
| 46 | +$client->assistants->create( |
| 47 | + new CreateAssistantDto([]), |
| 48 | +); |
| 49 | + |
| 50 | +``` |
| 51 | + |
| 52 | +## Environments |
| 53 | + |
| 54 | +This SDK allows you to configure different environments for API requests. |
| 55 | + |
| 56 | +```php |
| 57 | +The SDK defaults to the `Default_` environment. To use a different environment, pass it to the client constructor: |
| 58 | + |
| 59 | +```php |
| 60 | +use Vapi\VapiClient; |
| 61 | +use Vapi\Environments; |
| 62 | + |
| 63 | +$client = new VapiClient( |
| 64 | + token: '<YOUR_TOKEN>', |
| 65 | + options: [ |
| 66 | + 'baseUrl' => Environments::Staging->value |
| 67 | + ] |
| 68 | +); |
| 69 | +``` |
| 70 | + |
| 71 | +Available environments: |
| 72 | +- `Environments::Default_` |
| 73 | +``` |
| 74 | +
|
| 75 | +## Exception Handling |
| 76 | +
|
| 77 | +When the API returns a non-success status code (4xx or 5xx response), an exception will be thrown. |
| 78 | +
|
| 79 | +```php |
| 80 | +use Vapi\Exceptions\VapiApiException; |
| 81 | +use Vapi\Exceptions\VapiException; |
| 82 | +
|
| 83 | +try { |
| 84 | + $response = $client->assistants->create(...); |
| 85 | +} catch (VapiApiException $e) { |
| 86 | + echo 'API Exception occurred: ' . $e->getMessage() . "\n"; |
| 87 | + echo 'Status Code: ' . $e->getCode() . "\n"; |
| 88 | + echo 'Response Body: ' . $e->getBody() . "\n"; |
| 89 | + // Optionally, rethrow the exception or handle accordingly. |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +## Advanced |
| 94 | + |
| 95 | +### Custom Client |
| 96 | + |
| 97 | +This SDK is built to work with any HTTP client that implements the [PSR-18](https://www.php-fig.org/psr/psr-18/) `ClientInterface`. |
| 98 | +By default, if no client is provided, the SDK will use `php-http/discovery` to find an installed HTTP client. |
| 99 | +However, you can pass your own client that adheres to `ClientInterface`: |
| 100 | + |
| 101 | +```php |
| 102 | +use Vapi\VapiClient; |
| 103 | + |
| 104 | +// Pass any PSR-18 compatible HTTP client implementation. |
| 105 | +// For example, using Guzzle: |
| 106 | +$customClient = new \GuzzleHttp\Client([ |
| 107 | + 'timeout' => 5.0, |
| 108 | +]); |
| 109 | + |
| 110 | +$client = new VapiClient(options: [ |
| 111 | + 'client' => $customClient |
| 112 | +]); |
| 113 | + |
| 114 | +// Or using Symfony HttpClient: |
| 115 | +// $customClient = (new \Symfony\Component\HttpClient\Psr18Client()) |
| 116 | +// ->withOptions(['timeout' => 5.0]); |
| 117 | +// |
| 118 | +// $client = new VapiClient(options: [ |
| 119 | +// 'client' => $customClient |
| 120 | +// ]); |
| 121 | +``` |
| 122 | + |
| 123 | +### Retries |
| 124 | + |
| 125 | +The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long |
| 126 | +as the request is deemed retryable and the number of retry attempts has not grown larger than the configured |
| 127 | +retry limit (default: 2). |
| 128 | + |
| 129 | +A request is deemed retryable when any of the following HTTP status codes is returned: |
| 130 | + |
| 131 | +- [408](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408) (Timeout) |
| 132 | +- [429](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429) (Too Many Requests) |
| 133 | +- [5XX](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500) (Internal Server Errors) |
| 134 | + |
| 135 | +Use the `maxRetries` request option to configure this behavior. |
| 136 | + |
| 137 | +```php |
| 138 | +$response = $client->assistants->create( |
| 139 | + ..., |
| 140 | + options: [ |
| 141 | + 'maxRetries' => 0 // Override maxRetries at the request level |
| 142 | + ] |
| 143 | +); |
| 144 | +``` |
| 145 | + |
| 146 | +### Timeouts |
| 147 | + |
| 148 | +The SDK defaults to a 30 second timeout. Use the `timeout` option to configure this behavior. |
| 149 | + |
| 150 | +```php |
| 151 | +$response = $client->assistants->create( |
| 152 | + ..., |
| 153 | + options: [ |
| 154 | + 'timeout' => 3.0 // Override timeout at the request level |
| 155 | + ] |
| 156 | +); |
| 157 | +``` |
| 158 | + |
| 159 | +## Contributing |
| 160 | + |
| 161 | +While we value open-source contributions to this SDK, this library is generated programmatically. |
| 162 | +Additions made directly to this library would have to be moved over to our generation code, |
| 163 | +otherwise they would be overwritten upon the next generated release. Feel free to open a PR as |
| 164 | +a proof of concept, but know that we will not be able to merge it as-is. We suggest opening |
| 165 | +an issue first to discuss with us! |
| 166 | + |
| 167 | +On the other hand, contributions to the README are always very welcome! |
0 commit comments