|
| 1 | +# Vapi Python Library |
| 2 | + |
| 3 | +[](https://buildwithfern.com?utm_source=github&utm_medium=github&utm_campaign=readme&utm_source=https%3A%2F%2Fi.8713187.xyz%2Ffern-demo%2Fvapi-python-sdk) |
| 4 | +[](https://pypi.python.org/pypi/Vapi) |
| 5 | + |
| 6 | +The Vapi Python library provides convenient access to the Vapi API from Python. |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | +```sh |
| 11 | +pip install Vapi |
| 12 | +``` |
| 13 | + |
| 14 | +## Reference |
| 15 | + |
| 16 | +A full reference for this library is available [here](./reference.md). |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +Instantiate and use the client with the following: |
| 21 | + |
| 22 | +```python |
| 23 | +from vapi import Vapi |
| 24 | + |
| 25 | +client = Vapi( |
| 26 | + token="YOUR_TOKEN", |
| 27 | +) |
| 28 | +client.calls.create() |
| 29 | +``` |
| 30 | + |
| 31 | +## Async Client |
| 32 | + |
| 33 | +The SDK also exports an `async` client so that you can make non-blocking calls to our API. |
| 34 | + |
| 35 | +```python |
| 36 | +import asyncio |
| 37 | + |
| 38 | +from vapi import AsyncVapi |
| 39 | + |
| 40 | +client = AsyncVapi( |
| 41 | + token="YOUR_TOKEN", |
| 42 | +) |
| 43 | + |
| 44 | + |
| 45 | +async def main() -> None: |
| 46 | + await client.calls.create() |
| 47 | + |
| 48 | + |
| 49 | +asyncio.run(main()) |
| 50 | +``` |
| 51 | + |
| 52 | +## Exception Handling |
| 53 | + |
| 54 | +When the API returns a non-success status code (4xx or 5xx response), a subclass of the following error |
| 55 | +will be thrown. |
| 56 | + |
| 57 | +```python |
| 58 | +from vapi.core.api_error import ApiError |
| 59 | + |
| 60 | +try: |
| 61 | + client.calls.create(...) |
| 62 | +except ApiError as e: |
| 63 | + print(e.status_code) |
| 64 | + print(e.body) |
| 65 | +``` |
| 66 | + |
| 67 | +## Pagination |
| 68 | + |
| 69 | +Paginated requests will return a `SyncPager` or `AsyncPager`, which can be used as generators for the underlying object. |
| 70 | + |
| 71 | +```python |
| 72 | +from vapi import Vapi |
| 73 | + |
| 74 | +client = Vapi( |
| 75 | + token="YOUR_TOKEN", |
| 76 | +) |
| 77 | +response = client.logs.get() |
| 78 | +for item in response: |
| 79 | + yield item |
| 80 | +# alternatively, you can paginate page-by-page |
| 81 | +for page in response.iter_pages(): |
| 82 | + yield page |
| 83 | +``` |
| 84 | + |
| 85 | +## Advanced |
| 86 | + |
| 87 | +### Retries |
| 88 | + |
| 89 | +The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long |
| 90 | +as the request is deemed retriable and the number of retry attempts has not grown larger than the configured |
| 91 | +retry limit (default: 2). |
| 92 | + |
| 93 | +A request is deemed retriable when any of the following HTTP status codes is returned: |
| 94 | + |
| 95 | +- [408](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408) (Timeout) |
| 96 | +- [429](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429) (Too Many Requests) |
| 97 | +- [5XX](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500) (Internal Server Errors) |
| 98 | + |
| 99 | +Use the `max_retries` request option to configure this behavior. |
| 100 | + |
| 101 | +```python |
| 102 | +client.calls.create(..., request_options={ |
| 103 | + "max_retries": 1 |
| 104 | +}) |
| 105 | +``` |
| 106 | + |
| 107 | +### Timeouts |
| 108 | + |
| 109 | +The SDK defaults to a 60 second timeout. You can configure this with a timeout option at the client or request level. |
| 110 | + |
| 111 | +```python |
| 112 | + |
| 113 | +from vapi import Vapi |
| 114 | + |
| 115 | +client = Vapi( |
| 116 | + ..., |
| 117 | + timeout=20.0, |
| 118 | +) |
| 119 | + |
| 120 | + |
| 121 | +# Override timeout for a specific method |
| 122 | +client.calls.create(..., request_options={ |
| 123 | + "timeout_in_seconds": 1 |
| 124 | +}) |
| 125 | +``` |
| 126 | + |
| 127 | +### Custom Client |
| 128 | + |
| 129 | +You can override the `httpx` client to customize it for your use-case. Some common use-cases include support for proxies |
| 130 | +and transports. |
| 131 | +```python |
| 132 | +import httpx |
| 133 | +from vapi import Vapi |
| 134 | + |
| 135 | +client = Vapi( |
| 136 | + ..., |
| 137 | + httpx_client=httpx.Client( |
| 138 | + proxies="http://my.test.proxy.example.com", |
| 139 | + transport=httpx.HTTPTransport(local_address="0.0.0.0"), |
| 140 | + ), |
| 141 | +) |
| 142 | +``` |
| 143 | + |
| 144 | +## Contributing |
| 145 | + |
| 146 | +While we value open-source contributions to this SDK, this library is generated programmatically. |
| 147 | +Additions made directly to this library would have to be moved over to our generation code, |
| 148 | +otherwise they would be overwritten upon the next generated release. Feel free to open a PR as |
| 149 | +a proof of concept, but know that we will not be able to merge it as-is. We suggest opening |
| 150 | +an issue first to discuss with us! |
| 151 | + |
| 152 | +On the other hand, contributions to the README are always very welcome! |
0 commit comments