|
| 1 | +--- |
| 2 | +title: Retries with Backoff |
| 3 | +description: Automatically retry failures with exponential backoff |
| 4 | +--- |
| 5 | + |
| 6 | +<Markdown src="/snippets/pro-callout.mdx" /> |
| 7 | + |
| 8 | +Fern SDKs will automatically retry failed requests with exponential backoff. A request will be retried as |
| 9 | +long as the request is deemed retryable and the number of retry attempts has |
| 10 | +not grown larger than the configured retry limit. |
| 11 | + |
| 12 | +### Retryable status codes |
| 13 | + |
| 14 | +A request is deemed retryable when any of the following HTTP status codes is returned: |
| 15 | +- [408](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408) (Timeout) |
| 16 | +- [429](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429) (Too Many Requests) |
| 17 | +- [5XX](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500) (Internal Server Errors) |
| 18 | + |
| 19 | +Note that you can configure the list of retryable status codes as well. For example, |
| 20 | +if you want to remove the `429` status code from the list of retryable status codes, you can do so. |
| 21 | + |
| 22 | +### Overriding the retry limit |
| 23 | + |
| 24 | +By default, the SDK will retry a failed request up to 2 times. SDK users can override the global |
| 25 | +default retry limit when instantiating the client. |
| 26 | + |
| 27 | +<CodeBlocks> |
| 28 | + <CodeBlock title="TypeScript"> |
| 29 | + ```ts {4} |
| 30 | + import { ImdbClient } from "imdb"; |
| 31 | + |
| 32 | + const client = new ImdbClient({ |
| 33 | + maxRetries: 1 // overrides the default retry limit to 1 |
| 34 | + }); |
| 35 | + ``` |
| 36 | + </CodeBlock> |
| 37 | + <CodeBlock title="Python"> |
| 38 | + ```python {4, 8} |
| 39 | + from imdb.client import Imdb, AsyncImdb |
| 40 | + |
| 41 | + client = Imdb({ |
| 42 | + max_retries: 1 # overrides the default retry limit to 1 |
| 43 | + }) |
| 44 | + |
| 45 | + async_client = AsyncImdb({ |
| 46 | + max_retries: 1 # overrides the default retry limit to 1 |
| 47 | + }) |
| 48 | + ``` |
| 49 | + </CodeBlock> |
| 50 | + <CodeBlock title="Java"> |
| 51 | + ```java {4} |
| 52 | + import com.imdb.ImdbClient; |
| 53 | + |
| 54 | + ImdbClient client = new ImdbClient.Builder() |
| 55 | + .maxRetries(1) // overrides the default retry limit to 1 |
| 56 | + .build(); |
| 57 | + ``` |
| 58 | + </CodeBlock> |
| 59 | + <CodeBlock title="Go"> |
| 60 | + ```go {7} |
| 61 | + import ( |
| 62 | + imdbclient "github.com/fern-workos/workos-go/client" |
| 63 | + "github.com/fern-workos/workos-go/option" |
| 64 | + ) |
| 65 | + |
| 66 | + client := imdbclient.NewClient( |
| 67 | + option.WithMaxAttempts(1), // overrides the default retry limit to 1 |
| 68 | + ) |
| 69 | + ``` |
| 70 | + </CodeBlock> |
| 71 | +</CodeBlocks> |
| 72 | + |
| 73 | +It's also possible to override the retry limit on a per-request basis. |
| 74 | + |
| 75 | +<CodeBlocks> |
| 76 | + <CodeBlock title="TypeScript"> |
| 77 | + ```ts {2} |
| 78 | + client.movie.get("tt0111161", { |
| 79 | + maxRetries: 3 // overrides the default retry limit to 3 |
| 80 | + }); |
| 81 | + ``` |
| 82 | + </CodeBlock> |
| 83 | + <CodeBlock title="Python"> |
| 84 | + ```python {2} |
| 85 | + client.movie.get("tt0111161", { |
| 86 | + max_retries: 3 // overrides the default retry limit to 3 |
| 87 | + }) |
| 88 | + ``` |
| 89 | + </CodeBlock> |
| 90 | + <CodeBlock title="Java"> |
| 91 | + ```java {2} |
| 92 | + client.movie().get("tt0111161", RequestOptions.builder() |
| 93 | + .maxRetries(3) // overrides the default retry limit to 3 |
| 94 | + .build()); |
| 95 | + ``` |
| 96 | + </CodeBlock> |
| 97 | + <CodeBlock title="Go"> |
| 98 | + ```go {4} |
| 99 | + response, err := client.Movies.Get( |
| 100 | + ctx, |
| 101 | + "tt0111161", |
| 102 | + option.WithMaxAttempts(1), |
| 103 | + ) |
| 104 | + ``` |
| 105 | + </CodeBlock> |
| 106 | +</CodeBlocks> |
| 107 | + |
0 commit comments