UUIDs in REST APIs - cover art

Comparisons 14 min read

UUIDs in REST APIs

November 10, 2026 · 14 min read

REST APIs expose resources by identifier. UUIDs replace sequential integers to reduce enumeration risk and let clients generate ids before POST. Consistency matters: lowercase hyphenated strings in JSON, strict parsing in paths, and correlation headers that reuse the same format.

Why UUIDs in public APIs

UUID path parameters

Routes like GET /users/{userId} should validate UUID format before database lookup - return 400 not 404 for malformed strings. OpenAPI can document format: uuid. Framework regexes must accept a-f and A-F then normalize to lowercase in responses.

GET /v1/orders/550e8400-e29b-41d4-a716-446655440000
Accept: application/json

Correlation and idempotency headers

X-Request-Id, X-Correlation-Id, and Idempotency-Key often carry UUIDs. Document required format; reject invalid values early so logs stay searchable.

UUIDs in JSON request and response bodies

Encode as JSON strings, never as numbers. Arrays of UUID strings are common for batch operations. Validate with schema format: uuid and the UUID validator in CI fixtures. Generate examples via UUID generator.

Error responses and foreign keys

Return stable error codes; include the invalid id in machine-readable fields when safe. Foreign key errors should not echo internal sequential ids if your public contract is UUID-only.

FAQ

Should URLs use uppercase UUIDs?
RFC allows hex case-insensitive parsing; lowercase in docs and responses reduces confusion.
Can I use UUID v7 in APIs?
Yes where time-ordering helps listing; document version expectations for clients.
Are UUIDs in URLs logged?
Often yes - treat them as sensitive if they grant access without extra auth.

Related: UUID security considerations · UUID generation in Go · UUID generation in Java · How to validate UUIDs

Browse all tools