Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add Variables in URL #2415

Merged
merged 4 commits into from
Dec 12, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions pages/docs/concepts/asyncapi-document/variable-url.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
---
title: Server Variables
quetzalliwrites marked this conversation as resolved.
Show resolved Hide resolved
weight: 275
---

Server's URL consists of `host` and `pathname` fields. These values are not always known when you design your system. AsyncAPI enables you to construct dynamic URLs while enhancing the flexibility and maintainability of your AsyncAPI documents. These dynamic values (variables) are placeholders for values you can replace during runtime. This way you can easily manage multiple endpoints, handling various server configurations and environments.
quetzalliwrites marked this conversation as resolved.
Show resolved Hide resolved

## Add variables

You can add variables to `server.host` and `server.pathname` and you do it by adding a variable in between curly braces like `{braces}`. Next, you use `server.variables` to provide definitions of your variables. Leverage `components.serverVariables` to enable reusable variable definitions across multiple servers.
quetzalliwrites marked this conversation as resolved.
Show resolved Hide resolved

The diagram below describes how to use reusable variables in AsyncAPI.

```mermaid
graph LR
C[servers]
F[host]
I[protocol]
E[pathname]
A[components]
B[serverVariables]
D[variables]
C --> F
C --> I
C --> E
C --> D
D --> |$ref| B
A --> B

style C fill:#47BCEE,stroke:#000;
style D fill:#47BCEE,stroke:#000;
style F fill:#47BCEE,stroke:#000;
style E fill:#47BCEE,stroke:#000
```

You put variables in `host` and/or `pathname`. Next, you define reusable variables in `components.serverVariables`. Last, you make sure that `server.variables` from your server reference definitions from `components.serverVariables` using `$ref`.
quetzalliwrites marked this conversation as resolved.
Show resolved Hide resolved

### Servers section

Define the servers section in your AsyncAPI document, and include the `host` and `pathname` for your API servers. Use placeholders enclosed in curly braces {} to represent the variables in the server. For example:
quetzalliwrites marked this conversation as resolved.
Show resolved Hide resolved

```yaml
servers:
production:
host: '{subdomain}.example.com:{port}'
pathname: '/{version}
variables:
subdomain:
enum:
- development
- staging
- production
port:
default: '8080'
version:
enum:
- v1
- v2
```

### `serverVariables` section

Define the `components.serverVariables` section in your AsyncAPI document. For each variable used in the server `host` or `pathname`, provide a default value and an optional description. This helps you avoid repeating variable defination. For example:
quetzalliwrites marked this conversation as resolved.
Show resolved Hide resolved

```yaml
components:
serverVariables:
subdomain:
enum:
- development
- staging
- production
default: development
port:
default: '8080'
version:
enum:
- v1
- v2
```

### Define domain and port variables

Use `components.serverVariables` in your server using [Reference Object](/docs/reference/specification/v3.0.0#referenceObject) to avoid repeating the information:
quetzalliwrites marked this conversation as resolved.
Show resolved Hide resolved

```yml
variables:
subdomain:
$ref: '#/components/serverVariables/subdomain'
```

Here's the complete AsyncAPI document with the servers' variables for the `host` field:

```yaml
asyncapi: 3.0.0
info:
title: Example API
version: '1.0.0'
servers:
production:
host: '{subdomain}.example.com:{port}'
pathname: '/{version}'
protocol: amqp
variables:
subdomain:
$ref: '#/components/serverVariables/subdomain'
port:
$ref: '#/components/serverVariables/port'
version:
$ref: '#/components/serverVariables/version'
development:
host: '{subdomain}.dev.example.com:{port}'
pathname: /v1
protocol: amqp
variables:
subdomain:
$ref: '#/components/serverVariables/subdomain'
port:
$ref: '#/components/serverVariables/port'
version:
$ref: '#/components/serverVariables/version'
components:
serverVariables:
subdomain:
enum:
- development
- staging
- production
default: development
port:
default: '8080'
version:
enum:
- v1
- v2
```