|
| 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 API from PHP. |
| 7 | + |
| 8 | +## Requirements |
| 9 | + |
| 10 | +This SDK requires PHP ^8.1. |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +```sh |
| 15 | +composer require vapi/vapi |
| 16 | +``` |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +Instantiate and use the client with the following: |
| 21 | + |
| 22 | +```php |
| 23 | +<?php |
| 24 | + |
| 25 | +use Vapi\VapiClient; |
| 26 | +use Vapi\Calls\Requests\CreateCallDto; |
| 27 | + |
| 28 | +$client = new VapiClient(token: '<YOUR_TOKEN>'); |
| 29 | + |
| 30 | +$call = $client->calls->create( |
| 31 | + new CreateCallDto([ |
| 32 | + 'name' => 'My Call', |
| 33 | + 'assistantId' => 'your-assistant-id', |
| 34 | + ]) |
| 35 | +); |
| 36 | +``` |
| 37 | + |
| 38 | +## Exception Handling |
| 39 | + |
| 40 | +When the API returns a non-success status code (4xx or 5xx response), an exception will be thrown. |
| 41 | + |
| 42 | +```php |
| 43 | +use Vapi\Exceptions\VapiApiException; |
| 44 | +use Vapi\Exceptions\VapiException; |
| 45 | + |
| 46 | +try { |
| 47 | + $call = $client->calls->create(...); |
| 48 | +} catch (VapiApiException $e) { |
| 49 | + echo 'API Exception occurred: ' . $e->getMessage() . "\n"; |
| 50 | + echo 'Status Code: ' . $e->getCode() . "\n"; |
| 51 | + echo 'Response Body: ' . $e->getBody() . "\n"; |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +## Advanced |
| 56 | + |
| 57 | +### Custom Client |
| 58 | + |
| 59 | +This SDK is built to work with any HTTP client that implements Guzzle's `ClientInterface`. |
| 60 | +By default, if no client is provided, the SDK will use Guzzle's default HTTP client. |
| 61 | +However, you can pass your own client that adheres to `ClientInterface`: |
| 62 | + |
| 63 | +```php |
| 64 | +use Vapi\VapiClient; |
| 65 | + |
| 66 | +// Create a custom Guzzle client with specific configuration. |
| 67 | +$customClient = new \GuzzleHttp\Client([ |
| 68 | + 'timeout' => 5.0, |
| 69 | +]); |
| 70 | + |
| 71 | +// Pass the custom client when creating an instance of the class. |
| 72 | +$client = new VapiClient( |
| 73 | + token: '<YOUR_TOKEN>', |
| 74 | + options: [ |
| 75 | + 'client' => $customClient |
| 76 | + ] |
| 77 | +); |
| 78 | + |
| 79 | +// You can also utilize the same technique to leverage advanced customizations to the client such as adding middleware |
| 80 | +$handlerStack = \GuzzleHttp\HandlerStack::create(); |
| 81 | +$handlerStack->push(MyCustomMiddleware::create()); |
| 82 | +$customClient = new \GuzzleHttp\Client(['handler' => $handlerStack]); |
| 83 | + |
| 84 | +// Pass the custom client when creating an instance of the class. |
| 85 | +$client = new VapiClient( |
| 86 | + token: '<YOUR_TOKEN>', |
| 87 | + options: [ |
| 88 | + 'client' => $customClient |
| 89 | + ] |
| 90 | +); |
| 91 | +``` |
| 92 | + |
| 93 | +### Retries |
| 94 | + |
| 95 | +The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long |
| 96 | +as the request is deemed retryable and the number of retry attempts has not grown larger than the configured |
| 97 | +retry limit (default: 2). |
| 98 | + |
| 99 | +A request is deemed retryable when any of the following HTTP status codes is returned: |
| 100 | + |
| 101 | +- [408](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408) (Timeout) |
| 102 | +- [429](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429) (Too Many Requests) |
| 103 | +- [5XX](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500) (Internal Server Errors) |
| 104 | + |
| 105 | +Use the `maxRetries` request option to configure this behavior. |
| 106 | + |
| 107 | +```php |
| 108 | +$call = $client->calls->create( |
| 109 | + new CreateCallDto([...]), |
| 110 | + options: [ |
| 111 | + 'maxRetries' => 0 // Override maxRetries at the request level |
| 112 | + ] |
| 113 | +); |
| 114 | +``` |
| 115 | + |
| 116 | +### Timeouts |
| 117 | + |
| 118 | +Use the `timeout` option to configure request timeouts. |
| 119 | + |
| 120 | +```php |
| 121 | +$call = $client->calls->create( |
| 122 | + new CreateCallDto([...]), |
| 123 | + options: [ |
| 124 | + 'timeout' => 3.0 // Override timeout to 3 seconds |
| 125 | + ] |
| 126 | +); |
| 127 | +``` |
| 128 | + |
| 129 | +## Contributing |
| 130 | + |
| 131 | +While we value open-source contributions to this SDK, this library is generated programmatically. |
| 132 | +Additions made directly to this library would have to be moved over to our generation code, |
| 133 | +otherwise they would be overwritten upon the next generated release. Feel free to open a PR as |
| 134 | +a proof of concept, but know that we will not be able to merge it as-is. We suggest opening |
| 135 | +an issue first to discuss with us! |
| 136 | + |
| 137 | +On the other hand, contributions to the README are always very welcome! |
| 138 | + |
0 commit comments