Skip to article
TOOLS. SYSTEMS. BETTER DECISIONS.
Stack & Method

The journal

Correlation IDs in APIs: what they are and how to use them

A practical explanation of correlation IDs in APIs, with propagation tips, when to use tracing, pitfalls to avoid, and a hands-on checklist.

Editorial diagram of a request entering an API gateway with a highlighted correlation token flowing through multiple services and logs what is a correlation id in api

A correlation ID in an API is a unique identifier created at the first point a request enters your system and then passed unchanged through every downstream call. It lets you tie together all related logs and events for that single request, which makes troubleshooting much faster. This practice is widely recommended for distributed systems and is typically implemented via an HTTP header on requests and responses (https://microsoft.github.io/code-with-engineering-playbook/observability/correlation-id/).

Why correlation IDs matter

In multi-service or microservice architectures, understanding a single user transaction across components can be difficult. A correlation ID acts as the “glue” that binds the transaction together so you can query logs across services using one token (https://microsoft.github.io/code-with-engineering-playbook/observability/correlation-id/). Some public APIs also return the correlation ID in their responses so customers and support can reference a specific call when reporting issues. For example, PayPal includes a Correlation ID in every API response to help its support locate relevant logs (https://www.paypal.com/us/cshelp/article/what-is-the-%E2%80%9Ccorrelation-id%E2%80%9D-field-in-the-api-used-for-ts1087).

Minimal editorial diagram showing an HTTP request card with a masked correlation token pill forwarded by an arrow to a downstream service card on a pale blue background what is a correlation id in api

How to generate and propagate a correlation ID

Where the ID comes from

Assign a correlation ID as early as possible, typically at the API gateway or first service to receive the request. If the client provides one, you can accept it; if not, generate a unique value and attach it to the request context for use by all components. Ensure every service logs the same ID and forwards it on any outbound calls (https://microsoft.github.io/code-with-engineering-playbook/observability/correlation-id/).

Header names and response behavior

Use one consistent HTTP header to carry the value. The name X-Correlation-ID is commonly used in practice and returned in responses in real-world systems, such as the example described by Salesforce Engineering (https://engineering.salesforce.com/investigate-issues-with-ease-by-adding-a-correlation-id-to-your-api-7a689413ff44/). Microsoft’s guidance also recommends adding the ID to outgoing responses where possible (https://microsoft.github.io/code-with-engineering-playbook/observability/correlation-id/).

Ship a correlation ID in your API in under a day

Correlation IDs versus distributed tracing

Correlation IDs group all events for a single request. Distributed tracing adds structure and timing by linking spans into a trace tree (trace ID and span IDs). Tracing is ideal for latency analysis and understanding parent-child call relationships. Many teams use both: a correlation ID for log searches and a trace ID for deep performance diagnosis (https://last9.io/blog/correlation-id-vs-trace-id/). Microsoft also notes that OpenTelemetry provides end-to-end context propagation, with a TraceId serving as the correlation mechanism inside a tracing system (https://microsoft.github.io/code-with-engineering-playbook/observability/correlation-id/).

Side by side 2D vector diagram illustrating what is a correlation id in api left panel shows stacked log entries linked by a single highlighted correlation token right panel shows a trace tree with spans and horizontal timing bars on a soft blue background

Common pitfalls and quick fixes

Related reading on Stack & Method

FAQ

Should clients be allowed to set the correlation ID?

Many systems accept a client-provided ID if it’s present, otherwise they generate one. Some organizations choose to always generate their own at the edge to guarantee uniqueness and consistency; Salesforce describes this approach and returns the ID in responses (https://engineering.salesforce.com/investigate-issues-with-ease-by-adding-a-correlation-id-to-your-api-7a689413ff44/). Microsoft’s guidance emphasizes assigning the ID as early as possible and propagating it through all components (https://microsoft.github.io/code-with-engineering-playbook/observability/correlation-id/).

What format should the ID use?

A randomly generated UUID (such as UUID v4) is a practical default because it’s globally unique and easy to validate in logs (https://last9.io/blog/correlation-id-vs-trace-id/).

Should I include the correlation ID in responses?

It often helps. Microsoft recommends adding it to outgoing responses where possible, and PayPal’s public documentation shows they return a Correlation ID and use it for support lookups (https://microsoft.github.io/code-with-engineering-playbook/observability/correlation-id/, https://www.paypal.com/us/cshelp/article/what-is-the-%E2%80%9Ccorrelation-id%E2%80%9D-field-in-the-api-used-for-ts1087).

Stack & Method logo

Related guides

Sources and further reading

Your privacy choices

Optional analytics helps us improve these guides. It stays off until you allow it. Privacy details