PHP code example of vapi / vapi

1. Go to this page and download the library: Download vapi/vapi library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

vapi / vapi example snippets




namespace Example;

use Vapi\VapiClient;
use Vapi\Types\CreateAssistantDto;

$client = new VapiClient(
    token: '<token>',
);
$client->assistants->create(
    new CreateAssistantDto([]),
);


The SDK defaults to the `Default_` environment. To use a different environment, pass it to the client constructor:


use Vapi\Exceptions\VapiApiException;
use Vapi\Exceptions\VapiException;

try {
    $response = $client->assistants->create(...);
} catch (VapiApiException $e) {
    echo 'API Exception occurred: ' . $e->getMessage() . "\n";
    echo 'Status Code: ' . $e->getCode() . "\n";
    echo 'Response Body: ' . $e->getBody() . "\n";
    // Optionally, rethrow the exception or handle accordingly.
}

use Vapi\VapiClient;

// Pass any PSR-18 compatible HTTP client implementation.
// For example, using Guzzle:
$customClient = new \GuzzleHttp\Client([
    'timeout' => 5.0,
]);

$client = new VapiClient(options: [
    'client' => $customClient
]);

// Or using Symfony HttpClient:
// $customClient = (new \Symfony\Component\HttpClient\Psr18Client())
//     ->withOptions(['timeout' => 5.0]);
//
// $client = new VapiClient(options: [
//     'client' => $customClient
// ]);

$response = $client->assistants->create(
    ...,
    options: [
        'maxRetries' => 0 // Override maxRetries at the request level
    ]
);

$response = $client->assistants->create(
    ...,
    options: [
        'timeout' => 3.0 // Override timeout at the request level
    ]
);