|
1 |
| -# vapi-typescript-sdk |
2 |
| -The official TypeScript SDK for accessing Vapi's API |
| 1 | +# Vapi TypeScript Library |
| 2 | + |
| 3 | +[](https://github.com/fern-api/fern) |
| 4 | +[](https://www.npmjs.com/package/vapi) |
| 5 | + |
| 6 | +The Vapi TypeScript library provides convenient access to the Vapi API from TypeScript. |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | +```sh |
| 11 | +npm i -s vapi |
| 12 | +``` |
| 13 | + |
| 14 | +## Usage |
| 15 | + |
| 16 | +Instantiate and use the client with the following: |
| 17 | + |
| 18 | +```typescript |
| 19 | +import { VapiClient } from "vapi"; |
| 20 | + |
| 21 | +const client = new VapiClient({ token: "YOUR_TOKEN" }); |
| 22 | +await client.calls.create(); |
| 23 | +``` |
| 24 | + |
| 25 | +## Request And Response Types |
| 26 | + |
| 27 | +The SDK exports all request and response types as TypeScript interfaces. Simply import them with the |
| 28 | +following namespace: |
| 29 | + |
| 30 | +```typescript |
| 31 | +import { Vapi } from "vapi"; |
| 32 | + |
| 33 | +const request: Vapi.CallsListRequest = { |
| 34 | + ... |
| 35 | +}; |
| 36 | +``` |
| 37 | + |
| 38 | +## Exception Handling |
| 39 | + |
| 40 | +When the API returns a non-success status code (4xx or 5xx response), a subclass of the following error |
| 41 | +will be thrown. |
| 42 | + |
| 43 | +```typescript |
| 44 | +import { VapiError } from "vapi"; |
| 45 | + |
| 46 | +try { |
| 47 | + await client.calls.create(...); |
| 48 | +} catch (err) { |
| 49 | + if (err instanceof VapiError) { |
| 50 | + console.log(err.statusCode); |
| 51 | + console.log(err.message); |
| 52 | + console.log(err.body); |
| 53 | + } |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +## Advanced |
| 58 | + |
| 59 | +### Retries |
| 60 | + |
| 61 | +The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long |
| 62 | +as the request is deemed retriable and the number of retry attempts has not grown larger than the configured |
| 63 | +retry limit (default: 2). |
| 64 | + |
| 65 | +A request is deemed retriable when any of the following HTTP status codes is returned: |
| 66 | + |
| 67 | +- [408](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408) (Timeout) |
| 68 | +- [429](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429) (Too Many Requests) |
| 69 | +- [5XX](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500) (Internal Server Errors) |
| 70 | + |
| 71 | +Use the `maxRetries` request option to configure this behavior. |
| 72 | + |
| 73 | +```typescript |
| 74 | +const response = await client.calls.create(..., { |
| 75 | + maxRetries: 0 // override maxRetries at the request level |
| 76 | +}); |
| 77 | +``` |
| 78 | + |
| 79 | +### Timeouts |
| 80 | + |
| 81 | +The SDK defaults to a 60 second timeout. Use the `timeoutInSeconds` option to configure this behavior. |
| 82 | + |
| 83 | +```typescript |
| 84 | +const response = await client.calls.create(..., { |
| 85 | + timeoutInSeconds: 30 // override timeout to 30s |
| 86 | +}); |
| 87 | +``` |
| 88 | + |
| 89 | +### Aborting Requests |
| 90 | + |
| 91 | +The SDK allows users to abort requests at any point by passing in an abort signal. |
| 92 | + |
| 93 | +```typescript |
| 94 | +const controller = new AbortController(); |
| 95 | +const response = await client.calls.create(..., { |
| 96 | + abortSignal: controller.signal |
| 97 | +}); |
| 98 | +controller.abort(); // aborts the request |
| 99 | +``` |
| 100 | + |
| 101 | +### Runtime Compatibility |
| 102 | + |
| 103 | +The SDK defaults to `node-fetch` but will use the global fetch client if present. The SDK works in the following |
| 104 | +runtimes: |
| 105 | + |
| 106 | +- Node.js 18+ |
| 107 | +- Vercel |
| 108 | +- Cloudflare Workers |
| 109 | +- Deno v1.25+ |
| 110 | +- Bun 1.0+ |
| 111 | +- React Native |
| 112 | + |
| 113 | +### Customizing Fetch Client |
| 114 | + |
| 115 | +The SDK provides a way for your to customize the underlying HTTP client / Fetch function. If you're running in an |
| 116 | +unsupported environment, this provides a way for you to break glass and ensure the SDK works. |
| 117 | + |
| 118 | +```typescript |
| 119 | +import { VapiClient } from "vapi"; |
| 120 | + |
| 121 | +const client = new VapiClient({ |
| 122 | + ... |
| 123 | + fetcher: // provide your implementation here |
| 124 | +}); |
| 125 | +``` |
| 126 | + |
| 127 | +## Contributing |
| 128 | + |
| 129 | +While we value open-source contributions to this SDK, this library is generated programmatically. |
| 130 | +Additions made directly to this library would have to be moved over to our generation code, |
| 131 | +otherwise they would be overwritten upon the next generated release. Feel free to open a PR as |
| 132 | +a proof of concept, but know that we will not be able to merge it as-is. We suggest opening |
| 133 | +an issue first to discuss with us! |
| 134 | + |
| 135 | +On the other hand, contributions to the README are always very welcome! |
0 commit comments