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