> For the complete documentation index, see [llms.txt](https://docs.thewalletcrew.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.thewalletcrew.io/developers-guides/scripting/globals/fetch/fetch-cache-and-throttle.md).

# fetch cache and throttle

The Cache and Throttle options of fetch - the key rules, errors that are cached, the throttle members and the 429 answer of a denied call.

Two options of [fetch](https://docs.thewalletcrew.io/developers-guides/scripting/globals/fetch.md) protect the remote system and the request that waits for it. `Cache` keeps the result of a call for a duration, so that following calls do not contact the remote system. `Throttle` limits how many calls the tenant makes in a time window. This page states the rules of both, with their traps.

<details>

<summary><strong>Real-world examples</strong></summary>

* A pass data provider caches the profile of a customer for five minutes, so that each render of the pass does not call the CRM.
* A script limits its calls to a partner search API to 20 per minute for the whole tenant.

</details>

## Cache

Many handlers run on every pass creation or update, and the remote system rarely changes between two of them. The option is an object with two required members:

| Member         | Type   | Meaning                                      |
| -------------- | ------ | -------------------------------------------- |
| `CacheKey`     | string | The name of the entry.                       |
| `DurationInMs` | number | How long the entry is kept, in milliseconds. |

```js
const res = await fetch("https://api.example.com/catalog", {
  ThrowOnError: false,
  Cache: { CacheKey: "catalog", DurationInMs: 10 * 60 * 1000 }
});
```

The cache is kept for each tenant, so a key used by one tenant never reaches another, and it is shared by every handler and every script of that tenant. Three rules describe its behavior precisely.

**Only the `CacheKey` identifies an entry.** The URL, the method, the headers and the body are not part of the key. A script that fetches the profile of a customer must therefore put the customer identifier into the key, for example `"profile:" + entity.email`. A fixed key such as `"profile"` returns the first customer's answer to every following customer until the entry expires.

**Every returned result is cached, errors included.** With `ThrowOnError: false`, a `500` answer, a `404` or an empty body is kept for the whole duration like any other result. A script that does not want a failure to be remembered keeps `ThrowOnError` at `true` for that call, because a thrown error is never cached, or uses a short duration.

**A cached result is returned as it was**, including its `StatusCode`, so the code that follows handles a cached failure the same way as a fresh one.

## Throttle

A remote system often imposes a rate limit, and a burst of registrations can exceed it. The option is an object:

| Member                   | Type    | Default  | Meaning                                                             |
| ------------------------ | ------- | -------- | ------------------------------------------------------------------- |
| `PermitLimit`            | number  | required | The maximum number of calls in the window.                          |
| `WindowInMs`             | number  | `60000`  | The length of the sliding window, in milliseconds.                  |
| `ThrottleKey`            | string  | the URL  | Names the counter. Calls with the same key share the counter.       |
| `ThrowOnDenied`          | boolean | `false`  | When `true`, a denied call throws instead of returning a status.    |
| `OnDeniedRetryAfterInMs` | number  | `60000`  | The retry hint carried by the error when `ThrowOnDenied` is `true`. |

When the limit is reached and `ThrowOnDenied` is `false`, `fetch` does not contact the remote system and returns `{ StatusCode: 429, ResponseText: "" }`. The script handles it like a `429` from the remote system itself, which is why `ThrowOnError: false` is the natural companion of `Throttle`.

```js
const res = await fetch("https://api.example.com/search?q=" + encodeURIComponent(term), {
  ThrowOnError: false,
  Throttle: { PermitLimit: 20, WindowInMs: 60 * 1000, ThrottleKey: "partner-search" },
  Cache: { CacheKey: "search:" + term, DurationInMs: 5 * 60 * 1000 }
});

if (res.StatusCode === 429) {
  return null;
}
```

Three rules help to avoid mistakes. The counter is kept for each tenant and shared by all handlers. When no `ThrottleKey` is given, each distinct URL has its own counter: a URL that varies with the customer, as above, would then never be limited, so a fixed `ThrottleKey` is required to limit a whole API. The throttle is checked before the cache: a call answered from the cache still consumes a permit, so a cache does not reduce the number of permits used, and a low limit can deny a call that the cache could have answered. Finally, a denied answer is never cached, because the throttle answers before the cache is reached.

## Checking the result

A call answered from the cache returns the same `StatusCode` and `ResponseText` as the first call. When a value stays wrong after the remote system is fixed, a cached error answer is the first suspect: shorten the duration, or use a new `CacheKey`. When a call returns `429` with an empty body and the remote system was never contacted, the throttle denied it.

## FAQ

<details>

<summary><strong>Does a cached result survive a configuration change?</strong></summary>

Yes, until its duration ends. After changing the URL or the response handling of a cached call, the script uses a new `CacheKey` to get fresh answers immediately.

</details>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.thewalletcrew.io/developers-guides/scripting/globals/fetch/fetch-cache-and-throttle.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
