PHP code example of treeshainfotech / laravel-vapi

1. Go to this page and download the library: Download treeshainfotech/laravel-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/ */

    

treeshainfotech / laravel-vapi example snippets


use TreeshaInfotech\Vapi\Builders\CreateAssistantBuilder;
use TreeshaInfotech\Vapi\Facades\Vapi;
use Vapi\Types\CreateAssistantDtoFirstMessageMode;

$assistant = Vapi::assistants()->create(
    CreateAssistantBuilder::make()
        ->name('Support bot')
        ->firstMessage('Hi, how can I help?')
        ->firstMessageMode(CreateAssistantDtoFirstMessageMode::AssistantSpeaksFirst)
        ->build(),
);

$this->app->instance(\Psr\Http\Client\ClientInterface::class, $myPsr18Client);

use TreeshaInfotech\Vapi\Facades\Vapi;

Vapi::assistants();             // AssistantsClient
Vapi::calls();                  // CallsClient
Vapi::chats();                  // ChatsClient
Vapi::campaigns();              // CampaignsClient
Vapi::sessions();               // SessionsClient
Vapi::phoneNumbers();           // PhoneNumbersClient
Vapi::squads();                 // SquadsClient
Vapi::tools();                  // ToolsClient
Vapi::files();                  // FilesClient
Vapi::structuredOutputs();      // StructuredOutputsClient
Vapi::analytics();              // AnalyticsClient
Vapi::insight();                // InsightClient
Vapi::eval();                   // EvalClient
Vapi::observabilityScorecard(); // ObservabilityScorecardClient
Vapi::providerResources();      // ProviderResourcesClient

Vapi::client();                 // the underlying Vapi\VapiClient
Vapi::raw();                    // raw JSON access, see below

use TreeshaInfotech\Vapi\Builders\ListCallsRequestBuilder;

// List recent calls for an assistant
$calls = Vapi::calls()->list(
    ListCallsRequestBuilder::make()
        ->assistantId('asst_123')
        ->limit(25.0)
        ->createdAtGt(new DateTime('-7 days'))
        ->build(),
);

// Fetch one call
$call = Vapi::calls()->get('call_abc');
$call?->status; // string, see Vapi\Types\CallStatus for the enum values

use Vapi\Exceptions\VapiApiException;

try {
    $call = Vapi::calls()->get($id);
} catch (VapiApiException $exception) {
    $exception->getCode(); // HTTP status code
    $exception->getBody(); // decoded error body
}

use TreeshaInfotech\Vapi\Builders\CreateAssistantBuilder;
use TreeshaInfotech\Vapi\Builders\CreateCampaignBuilder;
use Vapi\Types\CreateAssistantDtoClientMessagesItem;

$dto = CreateAssistantBuilder::make()
    ->name('Support bot')
    ->clientMessages(
        CreateAssistantDtoClientMessagesItem::ConversationUpdate,
        CreateAssistantDtoClientMessagesItem::StatusUpdate,
    )
    ->build();

// Required DTO fields become typed make() arguments:
$campaign = CreateCampaignBuilder::make('August outreach')
    ->phoneNumberId('pn_1')
    ->build();

$bases = Vapi::raw()->get('/v2/knowledge-base', ['limit' => 10]);
$created = Vapi::raw()->post('/v2/knowledge-base', ['name' => 'Docs']);
Vapi::raw()->patch("/v2/knowledge-base/{$id}", ['name' => 'Docs v2']);
Vapi::raw()->delete("/v2/knowledge-base/{$id}");

Route::vapiWebhook('integrations/vapi');                       // macro
WebhookRoute::register('integrations/vapi');                   // static-analysis-friendly equivalent

Route::domain('hooks.example.com')->middleware('throttle:60,1')->group(function (): void {
    Route::vapiWebhook('vapi', 'tenant.vapi.webhook');         // custom name for multi-registration
});

use TreeshaInfotech\Vapi\Webhooks\WebhookProcessor;

final class MyVapiController
{
    public function __invoke(Request $request, WebhookProcessor $processor): JsonResponse
    {
        Log::info('vapi webhook', ['tenant' => $request->route('tenant')]);

        return $processor->handle($request);
    }
}

use TreeshaInfotech\Vapi\Events\EndOfCallReportReceived;

Event::listen(function (EndOfCallReportReceived $event): void {
    ProcessCallReport::dispatch($event->report);
});

use TreeshaInfotech\Vapi\Contracts\RespondsToVapiWebhooks;
use TreeshaInfotech\Vapi\Events\WebhookReceived;
use Vapi\Types\ServerMessageResponse;

final class VapiResponder implements RespondsToVapiWebhooks
{
    public function respond(WebhookReceived $webhook): ?ServerMessageResponse
    {
        // return a ServerMessageResponse to answer, or null for the default acknowledgement
    }
}

// In a service provider:
$this->app->bind(RespondsToVapiWebhooks::class, VapiResponder::class);

use GuzzleHttp\Psr7\Response;
use TreeshaInfotech\Vapi\Facades\Vapi;

$fake = Vapi::fake(new Response(200, [], '[]'));

// code under test
Vapi::calls()->list();

$fake->assertSentCount(1);
$fake->assertSent(fn ($request) => str_contains((string) $request->getUri(), '/call'));

use Illuminate\Support\Facades\Concurrency;

[$call, $assistant, $numbers] = Concurrency::run([
    fn () => Vapi::calls()->get($callId),
    fn () => Vapi::assistants()->get($assistantId),
    fn () => Vapi::phoneNumbers()->list(),
]);
sh
php artisan vendor:publish --tag=vapi-config