On this page
Choosing between an ID and a slug for an API resource URL is a decision about identity, not just appearance. An ID can remain stable when a resource is renamed; a slug makes a URL easier to read but introduces naming and migration rules. This guide compares both approaches and a combined alternative.
Key Takeaways
- Use a stable public ID when clients need durable references independent of a resource’s name.
- Use a slug as the identifier only if you can define its uniqueness, rename, and reuse rules.
- A combined URL can offer readability while using the ID to identify the resource.
- Neither an opaque ID nor a readable slug authorizes a request. Check access to the resolved resource on every request.
- Document the canonical URL, path parameters, error responses, and what happens when a slug changes.
API resource URL design: slug vs ID
Consider an article named “How to Cache” with the public ID art_7k3 and slug how-to-cache. The following are alternative designs for the same resource, not observed HTTP exchanges.
| Design | Example GET request | How the server identifies the article |
|---|---|---|
| ID | GET /v1/articles/art_7k3 | Look up the stable public ID. |
| Slug | GET /v1/articles/how-to-cache | Look up the slug in its defined scope. |
| Combined | GET /v1/articles/art_7k3/how-to-cache | Look up the ID; treat the slug as descriptive. |
Each successful response could include the same representation, for example {"id":"art_7k3","title":"How to Cache","slug":"how-to-cache"}. A client can store the ID as the resource reference even if it also displays or follows a readable URL.
The examples assume that art_7k3 is an API-facing identifier. It might map to a database key, but clients need not know how. Choosing a public identifier separately from a database key lets you change storage details without necessarily changing the API contract.
Advertisement
When to choose an ID
An ID-based URL is a good default for resources that can be renamed, have non-unique names, or are primarily consumed by software. A client can keep art_7k3 while the title and slug change.
A sequential database ID is straightforward to use, but it may expose information about ordering, creation patterns, or the existence of other records. It also couples the URL to that internal key if clients depend on it. A separate, opaque public ID can reduce that exposure and decouple the API from storage. It is not automatically secret or unguessable; those properties depend on how it is generated and handled.
Regardless of ID format, authenticate where appropriate and authorize access to the specific resource. An attacker who learns or guesses an ID must not gain access merely by putting it in a URL.
When to choose a slug
A slug-only URL can work well for public, human-oriented resources whose names are stable, such as a carefully managed documentation page. It is easy to recognize and share, but the slug is now part of the resource’s identity from the client’s perspective.
Define these rules before exposing the URL:
- Uniqueness scope: Must a slug be unique across all articles, or only within a tenant or collection? Enforce that rule in storage, not just in a preliminary application check.
- Collisions: Decide what happens when two names produce the same slug. For example, reject the second assignment with a documented conflict response or generate a distinct slug. Handle concurrent writes atomically.
- Normalization: Specify case sensitivity, allowed characters, Unicode handling, and how spaces and punctuation become slug characters. Clients must encode path segments correctly rather than concatenate arbitrary names into a URL.
- Renames: Decide whether an old URL redirects, remains an alias, or stops resolving. Retained aliases also need uniqueness rules.
- Reuse: If an old slug is assigned to a different resource, an existing link could silently lead to the wrong resource. Reserve old slugs if preserving that meaning matters.
A slug can disclose a title, project name, tenant name, or other sensitive information in URLs, logs, browser history, and analytics. A readable URL is not appropriate when the name itself should remain private.
When to combine an ID and a slug
A combined URL such as /v1/articles/art_7k3/how-to-cache can be useful when people share links but clients also need stable identity. In this design, look up art_7k3 and compare the supplied slug with the article’s current slug. Do not use the slug alone to select a different article if it disagrees with the ID.
The trade-off is an extra routing rule: the API must define what happens for a missing, stale, or incorrect slug. For a read request, one option is to redirect a stale slug to the canonical URL. Another is to serve the representation and return the canonical URL in documented response data. Choose one behavior and keep it consistent; clients should not have to infer it.
Advertisement
Migration example: renaming an article
Suppose the title changes from “How to Cache” to “Caching Basics,” and the current slug changes from how-to-cache to caching-basics. The ID remains art_7k3.
For the combined design, a documented GET policy could be:
GET /v1/articles/art_7k3/caching-basicsreturns the article with its current title and slug.GET /v1/articles/art_7k3/how-to-cacheresolves the ID, checks access, and returns a308 Permanent RedirectwithLocation: /v1/articles/art_7k3/caching-basicsfor an authorized request.- An unknown ID returns the API’s documented not-found response. A slug from another article does not change which ID is looked up.
A permanent redirect can be cached, so use it only if that permanence fits your rename and access policy. Check how your clients handle redirects, particularly for methods that change data; do not assume every client will follow them. You can instead reject stale slugs or return canonical URL information if that better suits the API.
For a slug-only design, preserving the old URL requires an alias or redirect mapping from how-to-cache to the same resource. Update the resource and its alias mapping atomically where possible, and reserve the old slug so it cannot later identify an unrelated article. If revealing an old title is sensitive, consider whether retaining that alias is acceptable.
Document the URL contract
In your API description, document each path parameter’s meaning, format, and uniqueness scope; distinguish the stable identifier from a mutable slug. Describe successful responses, authentication and authorization requirements, not-found and conflict behavior, and any redirect policy. Include examples showing a renamed resource, and tell clients whether to store an ID, follow a canonical URL, or both.
For an OpenAPI description, path-template parameters need corresponding parameter definitions, including their names, location, required status, and schemas. Also document the operation’s response codes and any Location header used for redirects. The current OpenAPI Specification and authoritative REST API design guidance were not available to consult for this article, so verify the exact syntax and version-specific requirements against your chosen specification and tooling before publishing the contract.
Common Mistakes
- Treating a slug as permanent without a policy. Renaming a resource can break stored links or integrations.
- Reusing old slugs casually. A bookmarked URL may resolve to the wrong resource rather than fail clearly.
- Calling an ID private because it is hard to read. IDs and slugs can leak; neither replaces authorization.
- Checking uniqueness only before insertion. Concurrent requests can still collide unless the storage layer enforces the rule.
- Leaving stale-slug behavior undocumented. Clients need to know whether to expect a redirect, an error, or a successful response.
Best Practices
Start with a stable public ID if clients will keep references over time. Add a slug when readability is valuable enough to justify the additional contract. Keep mutable presentation fields separate from identity, enforce uniqueness in the appropriate scope, and test renames, collisions, authorization failures, and redirect handling with representative clients.
Conclusion
Choose the identifier around the resource’s lifecycle. IDs favor stable references; slugs favor readability; combined URLs can provide both if the ID remains authoritative and stale-slug behavior is explicit.
What would your domain sell for?
A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.