Guide
Implementing OpenAPI 3.1 Link Objects for Hypermedia Navigation
Learn how to add OpenAPI 3.1 Link Objects to your API spec so responses include discoverable URLs for related resources.
Published by Tasadduq Burney
30 Jul 2025, 05:53 UTC
3 min94.5K views0

Desired outcome
Add Link Objects to an OpenAPI 3.1 document so that API responses contain discoverable, actionable URLs that clients can follow without hard‑coding endpoints.
Prerequisites
- An OpenAPI 3.0/3.1 YAML or JSON file that you can edit.
swagger-cliinstalled (npm package@apidevtools/swagger-cli) for validation.- A text editor or IDE that supports YAML/JSON.
- Optional: Swagger‑UI or Swagger‑Editor for visual inspection.
- Optional: OpenAPI Generator or similar tool to test client generation.
Procedure
-
Identify the operation and response where you want to expose a link. For example, a
GET /users/{userId}operation that returns a user object and you want to link to the user’s orders. -
Choose how to define the link:
- Inline inside the response schema, or
- As a reusable component under
components.linksand reference it with$ref.
-
Create the Link Object. Each link must contain either an
operationId(referring to another operation in the same document) or aurlfield. Use templated syntax for runtime parameters.Example – inline link:
paths: /users/{userId}: get: operationId: getUser summary: Retrieve a user parameters: - name: userId in: path required: true schema: type: string responses: '200': description: A user object content: application/json: schema: $ref: '#/components/schemas/User' links: userOrders: operationId: getUserOrders parameters: userId: $request.path.userIdExample – reusable component:
components: links: UserOrders: operationId: getUserOrders parameters: userId: $request.path.userId paths: /users/{userId}: get: operationId: getUser responses: '200': description: A user object content: application/json: schema: $ref: '#/components/schemas/User' links: userOrders: $ref: '#/components/links/UserOrders' -
Ensure the target operation exists and its parameters match the link’s templated variables. In the example above,
getUserOrdersshould have a path parameter{userId}. - Save the file and proceed to validation.
Expected checks
- Run validation:
swagger-cli validate path/to/openapi.yaml. The command should exit with status 0 and report no errors. - Open the spec in Swagger‑UI (
swagger-uioreditor.swagger.io) and verify that the response example shows alinkssection with a clickable URL. - If you generate a client (e.g.,
openapi-generator generate -i openapi.yaml -g javascript -o out), inspect the generated code for a method that builds the link URL using the supplied parameters.
Recovery options
- If validation fails because a referenced operationId is missing, either add the missing operation or replace
operationIdwith a staticurlvalue. - If a
$refto a component cannot be resolved, check the component path and ensure the component is defined undercomponents.links. - When template variables do not match any parameter in the source operation, rename the variable in the link to match the parameter name, or add the missing parameter to the operation.
- As a fallback, document the direct endpoint in the API description (e.g., in
info.description) so consumers can still access the related resource even if link tooling is unavailable.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.