|
| 1 | +# Vapi Java Library |
| 2 | +[](https://github.com/fern-api/fern) |
| 3 | + |
| 4 | +The Vapi Java SDK provides convenient access to the Vapi API from Java or Kotlin. |
| 5 | + |
| 6 | +## Installation |
| 7 | + |
| 8 | +### Gradle |
| 9 | + |
| 10 | +Add the dependency in your `build.gradle`: |
| 11 | + |
| 12 | +```groovy |
| 13 | +dependencies { |
| 14 | + implementation 'com.vapi:vapi-java:0.x.x' |
| 15 | +} |
| 16 | +``` |
| 17 | + |
| 18 | +### Maven |
| 19 | + |
| 20 | +Add the dependency in your `pom.xml`: |
| 21 | + |
| 22 | +```xml |
| 23 | +<dependency> |
| 24 | + <groupId>com.vapi</groupId> |
| 25 | + <artifactId>vapi-java</artifactId> |
| 26 | + <version>0.x.x</version> |
| 27 | +</dependency> |
| 28 | +``` |
| 29 | + |
| 30 | +## Usage |
| 31 | + |
| 32 | +Instantiate and use the client with the following: |
| 33 | + |
| 34 | +```java |
| 35 | +import com.vapi.api.Vapi; |
| 36 | + |
| 37 | +Vapi vapi = Vapi.builder() |
| 38 | + .token("YOUR_TOKEN") |
| 39 | + .build(); |
| 40 | + |
| 41 | +vapi.call.create(); |
| 42 | +``` |
| 43 | + |
| 44 | +## Exception Handling |
| 45 | + |
| 46 | +When the API returns a non-success status code (4xx or 5xx response), a subclass of `VapiException` |
| 47 | +will be thrown. |
| 48 | + |
| 49 | +```java |
| 50 | +import com.vapi.api.core.VapiException; |
| 51 | + |
| 52 | +try { |
| 53 | + vapi.call.create(); |
| 54 | +} catch (VapiException e) { |
| 55 | + System.out.println(e.message()); |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +The SDK also supports error handling for first class exceptions with strongly typed body fields. |
| 60 | + |
| 61 | +```java |
| 62 | +import com.vapi.api.errors.InvalidRequestError; |
| 63 | + |
| 64 | +try { |
| 65 | + vapi.call.create(); |
| 66 | +} catch (InvalidRequestError e) { |
| 67 | + System.out.println(e.message()); |
| 68 | + System.out.println(e.getBody().getMissingField()); |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +## Advanced |
| 73 | + |
| 74 | +### Callbacks |
| 75 | + |
| 76 | +You can register `callbacks` with the Vapi client and the callbacks will be invoked |
| 77 | +after events have been sent or failed to send. |
| 78 | + |
| 79 | +```java |
| 80 | +vapi.addCallback((event, statusCode) -> { |
| 81 | + if (statusCode >= 400) { |
| 82 | + // do something with event in error case |
| 83 | + } |
| 84 | +}); |
| 85 | +``` |
| 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 | +## Contributing |
| 100 | + |
| 101 | +While we value open-source contributions to this SDK, this library is generated programmatically. |
| 102 | +Additions made directly to this library would have to be moved over to our generation code, |
| 103 | +otherwise they would be overwritten upon the next generated release. Feel free to open a PR as |
| 104 | +a proof of concept, but know that we will not be able to merge it as-is. We suggest opening |
| 105 | +an issue first to discuss with us! |
| 106 | + |
| 107 | +On the other hand, contributions to the README are always very welcome! |
0 commit comments