> For the complete documentation index, see [llms.txt](https://docs.tryterra.co/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.tryterra.co/faq/help-topics/data-api-sdk/historical-data-and-backfill/historical-fetch-size-timeouts-504-413-500.md).

# Why does a historical fetch return a 504 timeout?

Large synchronous [historical pulls](https://docs.tryterra.co/health-and-fitness-api/managing-user-health-data/requesting-historical-data) (`to_webhook=false`) can exceed the response-size limit or the \~30s gateway timeout, returning `504` / `413` / `500`, especially with many activities or `with_samples=true` (heavy GPS/HR series).

What to do:

* **Chunk the request into smaller date ranges** (e.g. several 3-10 day or month-by-month requests) for faster parallel responses, or reduce the range.
* Use `with_samples=false` for summaries.
* Best of all, **use `to_webhook=true`** to return immediately and deliver data asynchronously to your webhook without the size limit.

Within a request, data is returned in chronological order. For latency-sensitive flows, fetch-on-connect and cache, then rely on webhooks. Elevated latency on read endpoints is usually transient infra load, so retry after a short period.

`504`s shown in payload history (versus on your request) mean your server is not acknowledging fast enough, so process payloads asynchronously and return a quick `200`.

{% hint style="info" %}
`end_date` is exclusive, and you cannot request only specific sample types (all or none).
{% endhint %}


---

# 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.tryterra.co/faq/help-topics/data-api-sdk/historical-data-and-backfill/historical-fetch-size-timeouts-504-413-500.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
