On this page
A URI template describes how to construct a URI from named values. In API documentation, it can show how one endpoint changes with a resource identifier, search term, or optional parameter without listing every possible URL. This guide explains common RFC 6570 expansions and how to document their constraints.
Key Takeaways
- A template such as
https://api.example.com/users/{userId}is a pattern, not necessarily a URL a client can request directly. - The operator in an expression controls separators and encoding. For example,
{id}and{+id}can produce different results from the same value. - Undefined variables are omitted during expansion; whether an API requires a variable is a separate documentation and validation decision.
- Document allowed values and encoding rules alongside the template, and use an RFC 6570-compatible implementation rather than substituting strings by hand.
URI templates, URLs, and routes
A URI is an identifier; a URL is a URI that identifies a resource by its location. A URI template is a pattern from which a URI can be expanded. For example:
Template: https://api.example.com/users/{userId}
Value: userId = 42
Result: https://api.example.com/users/42
The braces are template syntax: expand the expression before making a request. A template containing no expressions can itself be a valid URI.
A server-side route pattern is different. A framework might use a route such as /users/:userId to match an incoming request and extract a parameter. That syntax and its matching behavior are framework-specific; it is not an RFC 6570 template. A URI template describes expansion, not necessarily how a server matches, validates, or authorizes the resulting request.
Advertisement
URI template variables and operators
An expression contains one or more variable names, optionally preceded by an operator. RFC 6570 defines several operators; the examples below use familiar string values unless noted otherwise.
| Expression | Purpose | Example value | Expansion |
|---|---|---|---|
{var} | Simple expansion | var = value | value |
{+path} | Reserved expansion | path = /foo/bar | /foo/bar |
{#path} | Fragment expansion | path = /foo/bar | #/foo/bar |
{/var} | Path-segment expansion | var = value | /value |
{;var} | Path-style parameter | var = value | ;var=value |
{?var} | Start a query string | var = value | ?var=value |
{&var} | Continue a query string | var = value | &var=value |
For comparison, simple expansion of the same path value, {path}, produces %2Ffoo%2Fbar: the slashes are encoded rather than preserved as path separators. Reserved expansion permits reserved characters to remain in the result, so use it only when that structure is intentional.
RFC 6570 also supports operators and modifiers beyond these examples, including list and associative-array expansion. Use a conforming library and consult the standard for the exact behavior needed by your API. This overview is based on RFC 6570, but the RFC could not be retrieved in the preparation environment; the examples have not been executed. Check the standard and your library's documentation before relying on edge-case behavior.
Document path and query values
A path value
Template: https://api.example.com/users/{userId}
Value: userId = team/blue
Result: https://api.example.com/users/team%2Fblue
Simple expansion encodes the slash in the value. That does not guarantee the server treats it as part of one identifier: proxies and frameworks can differ in how they handle encoded slashes. If identifiers may contain slashes, document and verify behavior across the request path, or choose an identifier format that avoids them.
A path-segment expression can also add the slash itself:
Template: https://api.example.com/users{/userId}
Value: userId = 42
Result: https://api.example.com/users/42
If userId is undefined, the latter expression contributes nothing. That does not make the identifier optional in your API contract; state explicitly whether it is required.
Query values and optional parameters
Use a query expression to include parameters only when their variables are defined:
Template: https://api.example.com/search{?q,limit}
Values: q = red & blue; limit = 10
Result: https://api.example.com/search?q=red%20%26%20blue&limit=10
If limit is undefined, the result is https://api.example.com/search?q=red%20%26%20blue. If both variables are undefined, the result is https://api.example.com/search. An explicitly defined empty string is different from an undefined variable: with an empty q and undefined limit, the query expansion is ?q=.
Use {&page} to add a parameter when a query string is already present. For example, https://api.example.com/items?sort=name{&page} with page = 2 expands to https://api.example.com/items?sort=name&page=2. If the preceding query is itself optional, prefer a single expression such as {?sort,page} rather than assuming {&page} will introduce the question mark.
Repeated path segments
The explode modifier * changes how a list is expanded. For example:
Template: https://api.example.com/files{/parts*}
Value: parts = [red, green, blue]
Result: https://api.example.com/files/red/green/blue
Without *, {/parts} expands that list as /red,green,blue. Document whether a variable is a string, list, or associative array: its type affects the output.
Advertisement
Specify the contract around the template
The template describes expansion, not the full API contract. For each variable, document:
- Requirement and type: whether it must be supplied, and whether it is a string, number, list, or associative array.
- Allowed values: constraints such as an enumerated set, length limit, or identifier format. Explain defaults separately from omission.
- Encoding and reserved characters: which operator is used, whether characters such as
/,?,&, and#are data or intended URI structure, and any server-specific handling of encoded characters. - Examples: show an ordinary value, an omitted optional value, and a value containing a space or reserved character where relevant.
For instance, an API can specify that limit is an optional integer from 1 through 100, with a default of 20 when omitted. Those constraints are documentation and server-validation rules; {?limit} alone does not enforce them.
Use a compatible expansion library and pass values in the form its documentation expects. Avoid manually concatenating values or indiscriminately pre-encoding them: either can produce malformed or unexpectedly encoded requests. Test expansion and the server's interpretation together, especially for non-ASCII text and reserved characters.
Common Mistakes
- Treating the template as a request URL. Expand its expressions before sending the request.
- Assuming missing means valid. Expansion can omit an undefined variable even when the API requires it.
- Using
{+var}as a shortcut for encoding. Reserved characters may alter the path, query, or fragment structure; reserved expansion is not input validation. - Confusing an empty value with an omitted value. They can expand differently and may have different API semantics.
- Assuming route syntax is portable. Framework route patterns and URI templates serve different purposes.
Best Practices
Keep templates close to the endpoint's parameter definitions and provide concrete expanded URLs. Validate required fields and allowed values on the server; a template or client-side expansion does not enforce authorization or input safety. Add examples to integration tests using the same template library and infrastructure your clients will use.
Conclusion
URI templates make endpoint patterns reusable, but the expressions describe expansion rather than validation or routing. Document the variable contract and encoding choices, then verify the resulting requests against your actual API stack.
What would your domain sell for?
A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.