|
| 1 | +# Skyvern TypeScript Library |
| 2 | + |
| 3 | +[](https://buildwithfern.com?utm_source=github&utm_medium=github&utm_campaign=readme&utm_source=https%3A%2F%2Fgithub.com%2FSkyvern-AI%2Fskyvern-typescript) |
| 4 | +[](https://www.npmjs.com/package/@skyvern/client) |
| 5 | + |
| 6 | +The Skyvern TypeScript library provides convenient access to the Skyvern APIs from TypeScript. |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | +```sh |
| 11 | +npm i -s @skyvern/client |
| 12 | +``` |
| 13 | + |
| 14 | +## Reference |
| 15 | + |
| 16 | +A full reference for this library is available [here](https://github.com/Skyvern-AI/skyvern-typescript/blob/HEAD/./reference.md). |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +Instantiate and use the client with the following: |
| 21 | + |
| 22 | +```typescript |
| 23 | +import { SkyvernClient } from "@skyvern/client"; |
| 24 | + |
| 25 | +const client = new SkyvernClient({ xApiKey: "YOUR_X_API_KEY", apiKey: "YOUR_API_KEY" }); |
| 26 | +await client.runTask({ |
| 27 | + "x-user-agent": "x-user-agent", |
| 28 | + body: { |
| 29 | + prompt: "Find the top 3 posts on Hacker News." |
| 30 | + } |
| 31 | +}); |
| 32 | +``` |
| 33 | + |
| 34 | +## Request And Response Types |
| 35 | + |
| 36 | +The SDK exports all request and response types as TypeScript interfaces. Simply import them with the |
| 37 | +following namespace: |
| 38 | + |
| 39 | +```typescript |
| 40 | +import { Skyvern } from "@skyvern/client"; |
| 41 | + |
| 42 | +const request: Skyvern.RunTaskRequest = { |
| 43 | + ... |
| 44 | +}; |
| 45 | +``` |
| 46 | + |
| 47 | +## Exception Handling |
| 48 | + |
| 49 | +When the API returns a non-success status code (4xx or 5xx response), a subclass of the following error |
| 50 | +will be thrown. |
| 51 | + |
| 52 | +```typescript |
| 53 | +import { SkyvernError } from "@skyvern/client"; |
| 54 | + |
| 55 | +try { |
| 56 | + await client.runTask(...); |
| 57 | +} catch (err) { |
| 58 | + if (err instanceof SkyvernError) { |
| 59 | + console.log(err.statusCode); |
| 60 | + console.log(err.message); |
| 61 | + console.log(err.body); |
| 62 | + console.log(err.rawResponse); |
| 63 | + } |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +## Advanced |
| 68 | + |
| 69 | +### Additional Headers |
| 70 | + |
| 71 | +If you would like to send additional headers as part of the request, use the `headers` request option. |
| 72 | + |
| 73 | +```typescript |
| 74 | +const response = await client.runTask(..., { |
| 75 | + headers: { |
| 76 | + 'X-Custom-Header': 'custom value' |
| 77 | + } |
| 78 | +}); |
| 79 | +``` |
| 80 | + |
| 81 | +### Additional Query String Parameters |
| 82 | + |
| 83 | +If you would like to send additional query string parameters as part of the request, use the `queryParams` request option. |
| 84 | + |
| 85 | +```typescript |
| 86 | +const response = await client.runTask(..., { |
| 87 | + queryParams: { |
| 88 | + 'customQueryParamKey': 'custom query param value' |
| 89 | + } |
| 90 | +}); |
| 91 | +``` |
| 92 | + |
| 93 | +### Retries |
| 94 | + |
| 95 | +The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long |
| 96 | +as the request is deemed retryable and the number of retry attempts has not grown larger than the configured |
| 97 | +retry limit (default: 2). |
| 98 | + |
| 99 | +A request is deemed retryable when any of the following HTTP status codes is returned: |
| 100 | + |
| 101 | +- [408](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408) (Timeout) |
| 102 | +- [429](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429) (Too Many Requests) |
| 103 | +- [5XX](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500) (Internal Server Errors) |
| 104 | + |
| 105 | +Use the `maxRetries` request option to configure this behavior. |
| 106 | + |
| 107 | +```typescript |
| 108 | +const response = await client.runTask(..., { |
| 109 | + maxRetries: 0 // override maxRetries at the request level |
| 110 | +}); |
| 111 | +``` |
| 112 | + |
| 113 | +### Timeouts |
| 114 | + |
| 115 | +The SDK defaults to a 60 second timeout. Use the `timeoutInSeconds` option to configure this behavior. |
| 116 | + |
| 117 | +```typescript |
| 118 | +const response = await client.runTask(..., { |
| 119 | + timeoutInSeconds: 30 // override timeout to 30s |
| 120 | +}); |
| 121 | +``` |
| 122 | + |
| 123 | +### Aborting Requests |
| 124 | + |
| 125 | +The SDK allows users to abort requests at any point by passing in an abort signal. |
| 126 | + |
| 127 | +```typescript |
| 128 | +const controller = new AbortController(); |
| 129 | +const response = await client.runTask(..., { |
| 130 | + abortSignal: controller.signal |
| 131 | +}); |
| 132 | +controller.abort(); // aborts the request |
| 133 | +``` |
| 134 | + |
| 135 | +### Access Raw Response Data |
| 136 | + |
| 137 | +The SDK provides access to raw response data, including headers, through the `.withRawResponse()` method. |
| 138 | +The `.withRawResponse()` method returns a promise that results to an object with a `data` and a `rawResponse` property. |
| 139 | + |
| 140 | +```typescript |
| 141 | +const { data, rawResponse } = await client.runTask(...).withRawResponse(); |
| 142 | + |
| 143 | +console.log(data); |
| 144 | +console.log(rawResponse.headers['X-My-Header']); |
| 145 | +``` |
| 146 | + |
| 147 | +### Runtime Compatibility |
| 148 | + |
| 149 | + |
| 150 | +The SDK works in the following runtimes: |
| 151 | + |
| 152 | + |
| 153 | + |
| 154 | +- Node.js 18+ |
| 155 | +- Vercel |
| 156 | +- Cloudflare Workers |
| 157 | +- Deno v1.25+ |
| 158 | +- Bun 1.0+ |
| 159 | +- React Native |
| 160 | + |
| 161 | +### Customizing Fetch Client |
| 162 | + |
| 163 | +The SDK provides a way for you to customize the underlying HTTP client / Fetch function. If you're running in an |
| 164 | +unsupported environment, this provides a way for you to break glass and ensure the SDK works. |
| 165 | + |
| 166 | +```typescript |
| 167 | +import { SkyvernClient } from "@skyvern/client"; |
| 168 | + |
| 169 | +const client = new SkyvernClient({ |
| 170 | + ... |
| 171 | + fetcher: // provide your implementation here |
| 172 | +}); |
| 173 | +``` |
| 174 | + |
| 175 | +## Contributing |
| 176 | + |
| 177 | +While we value open-source contributions to this SDK, this library is generated programmatically. |
| 178 | +Additions made directly to this library would have to be moved over to our generation code, |
| 179 | +otherwise they would be overwritten upon the next generated release. Feel free to open a PR as |
| 180 | +a proof of concept, but know that we will not be able to merge it as-is. We suggest opening |
| 181 | +an issue first to discuss with us! |
| 182 | + |
| 183 | +On the other hand, contributions to the README are always very welcome! |
0 commit comments