2017-04-04 16:33:22 +00:00
---
layout: api
page_title: Session - HTTP API
2020-04-07 18:55:19 +00:00
description: 'The /session endpoints create, destroy, and query sessions in Consul.'
2017-04-04 16:33:22 +00:00
---
# Session HTTP Endpoint
2019-11-25 17:07:04 +00:00
The `/session` endpoints create, destroy, and query sessions in Consul.
2017-04-04 16:33:22 +00:00
## Create Session
This endpoint initializes a new session. Sessions must be associated with a
node and may be associated with any number of checks.
2020-04-06 20:27:35 +00:00
| Method | Path | Produces |
| ------ | ----------------- | ------------------ |
| `PUT` | `/session/create` | `application/json` |
2017-04-04 16:33:22 +00:00
The table below shows this endpoint's support for
2023-01-25 16:52:43 +00:00
[blocking queries](/consul/api-docs/features/blocking),
[consistency modes](/consul/api-docs/features/consistency),
[agent caching](/consul/api-docs/features/caching), and
[required ACLs](/consul/api-docs/api-structure#authentication).
2017-04-04 16:33:22 +00:00
2018-09-06 10:34:28 +00:00
| Blocking Queries | Consistency Modes | Agent Caching | ACL Required |
| ---------------- | ----------------- | ------------- | --------------- |
| `NO` | `none` | `none` | `session:write` |
2017-04-04 16:33:22 +00:00
2022-05-10 15:51:11 +00:00
### Query Parameters
2017-04-04 16:33:22 +00:00
2022-05-10 15:51:11 +00:00
<!-- What's the relationship to ServiceChecks[].Namespace (if any)? -->
2020-04-23 22:13:18 +00:00
- `ns` `(string: "")` <EnterpriseAlert inline /> - Specifies the namespace to query.
2022-05-10 15:51:11 +00:00
You can also [specify the namespace through other methods](#methods-to-specify-namespace).
2019-11-25 17:07:04 +00:00
2017-04-04 16:33:22 +00:00
- `dc` `(string: "")` - Specifies the datacenter to query. This will default to
the datacenter of the agent being queried. This is specified as part of the
2022-05-10 15:51:11 +00:00
URL as a query parameter. Using this parameter across datacenters is not recommended.
### JSON Request Body Schema
2017-04-04 16:33:22 +00:00
2019-11-25 17:07:04 +00:00
- `LockDelay` `(string: "15s")` - Specifies the duration for the lock delay. This
must be greater than `0`.
2017-04-04 16:33:22 +00:00
2022-05-10 15:51:11 +00:00
- `Node` `(string: "<agent>")` - Specifies the name of the node.
This field must refer to a node that is already registered.
2017-04-04 16:33:22 +00:00
- `Name` `(string: "")` - Specifies a human-readable name for the session.
2021-07-20 06:52:21 +00:00
- `Checks` `(array<string>: nil)` - Specifies a list of node health
2018-12-11 17:00:52 +00:00
check IDs (commonly `CheckID` in API responses). It is highly recommended that,
if you override this list, you include the default `serfHealth`.
2021-07-20 06:52:21 +00:00
Deprecated in Consul 1.7.0 and replaced by `NodeChecks`. This field will be
omitted from the API response in Consul 1.7+ if `serfHealth` is the only defined check.
- `NodeChecks` `(array<string>: nil)` - Specifies a list of node health
check IDs (commonly `CheckID` in API responses). It is highly recommended that,
if you override this list, you include the default `serfHealth`. Added in
Consul 1.7.0.
2021-08-03 16:57:31 +00:00
- `ServiceChecks` `(array<ServiceCheck>: nil)` - Specifies a list of service
checks. Added in Consul 1.7.0. A service check has the following fields:
- `ID` `(string: <required>)` - The ID of the service check to monitor
(commonly `CheckID` in API responses).
- `Namespace` `(string: "")` <EnterpriseAlert inline /> - Specifies the
namespace in which to resolve the service check ID.
2017-04-04 16:33:22 +00:00
- `Behavior` `(string: "release")` - Controls the behavior to take when a
session is invalidated. Valid values are:
- `release` - causes any locks that are held to be released
2017-04-26 08:09:02 +00:00
- `delete` - causes any locks that are held to be deleted
2017-04-04 16:33:22 +00:00
2020-06-18 17:53:43 +00:00
- `TTL` `(string: "")` - Specifies the duration of a session (between 10s and
2017-04-04 16:33:22 +00:00
86400s). If provided, the session is invalidated if it is not renewed before
the TTL expires. The lowest practical TTL should be used to keep the number of
2019-04-03 15:54:21 +00:00
managed sessions low. When locks are forcibly expired, such as when following
2023-01-25 16:52:43 +00:00
the [leader election pattern](/consul/tutorials/developer-configuration/application-leader-elections) in an application,
2019-04-03 15:54:21 +00:00
sessions may not be reaped for up to double this TTL, so long TTL
2020-06-18 17:53:43 +00:00
values (> 1 hour) should be avoided. Valid time units include "s", "m" and "h".
2017-04-04 16:33:22 +00:00
### Sample Payload
```json
{
"LockDelay": "15s",
"Name": "my-service-lock",
"Node": "foobar",
"Checks": ["a", "b", "c"],
"Behavior": "release",
"TTL": "30s"
}
```
### Sample Request
2020-05-19 18:32:38 +00:00
```shell-session
2017-04-04 16:33:22 +00:00
$ curl \
--request PUT \
--data @payload.json \
2018-08-28 16:07:15 +00:00
http://127.0.0.1:8500/v1/session/create
2017-04-04 16:33:22 +00:00
```
### Sample Response
2021-07-20 06:52:21 +00:00
```json
2017-04-04 16:33:22 +00:00
{
"ID": "adf4238a-882b-9ddc-4a9d-5b6758e4159e"
}
```
- `ID` - the ID of the created session
## Delete Session
This endpoint destroys the session with the given name. If the session UUID is
malformed, an error is returned. If the session UUID does not exist or already
expired, a 200 is still returned (the operation is idempotent).
2020-04-06 20:27:35 +00:00
| Method | Path | Produces |
| :----- | :----------------------- | ------------------ |
| `PUT` | `/session/destroy/:uuid` | `application/json` |
2017-04-04 16:33:22 +00:00
Even though the Content-Type is `application/json`, the response is
either a literal `true` or `false`, indicating of whether the destroy was
successful.
The table below shows this endpoint's support for
2023-01-25 16:52:43 +00:00
[blocking queries](/consul/api-docs/features/blocking),
[consistency modes](/consul/api-docs/features/consistency),
[agent caching](/consul/api-docs/features/caching), and
[required ACLs](/consul/api-docs/api-structure#authentication).
2017-04-04 16:33:22 +00:00
2018-09-06 10:34:28 +00:00
| Blocking Queries | Consistency Modes | Agent Caching | ACL Required |
| ---------------- | ----------------- | ------------- | --------------- |
| `NO` | `none` | `none` | `session:write` |
2017-04-04 16:33:22 +00:00
2022-05-10 15:51:11 +00:00
### Path Parameters
2017-04-04 16:33:22 +00:00
- `uuid` `(string: <required>)` - Specifies the UUID of the session to destroy.
This is required and is specified as part of the URL path.
2022-05-10 15:51:11 +00:00
### Query Parameters
2017-04-04 16:33:22 +00:00
- `dc` `(string: "")` - Specifies the datacenter to query. This will default to
2022-05-10 15:51:11 +00:00
the datacenter of the agent being queried.
Using this parameter across datacenters is not recommended.
2020-04-06 20:27:35 +00:00
2020-04-23 22:13:18 +00:00
- `ns` `(string: "")` <EnterpriseAlert inline /> - Specifies the namespace to query.
2022-05-10 15:51:11 +00:00
You can also [specify the namespace through other methods](#methods-to-specify-namespace).
2017-04-04 16:33:22 +00:00
### Sample Request
2020-05-19 18:32:38 +00:00
```shell-session
2017-04-04 16:33:22 +00:00
$ curl \
2019-06-20 14:48:56 +00:00
--request PUT \
2018-08-28 16:07:15 +00:00
http://127.0.0.1:8500/v1/session/destroy/adf4238a-882b-9ddc-4a9d-5b6758e4159e
2017-04-04 16:33:22 +00:00
```
### Sample Response
```json
true
```
## Read Session
This endpoint returns the requested session information.
2020-04-06 20:27:35 +00:00
| Method | Path | Produces |
| :----- | :-------------------- | ------------------ |
| `GET` | `/session/info/:uuid` | `application/json` |
2017-04-04 16:33:22 +00:00
The table below shows this endpoint's support for
2023-01-25 16:52:43 +00:00
[blocking queries](/consul/api-docs/features/blocking),
[consistency modes](/consul/api-docs/features/consistency),
[agent caching](/consul/api-docs/features/caching), and
[required ACLs](/consul/api-docs/api-structure#authentication).
2017-04-04 16:33:22 +00:00
2018-09-06 10:34:28 +00:00
| Blocking Queries | Consistency Modes | Agent Caching | ACL Required |
| ---------------- | ----------------- | ------------- | -------------- |
| `YES` | `all` | `none` | `session:read` |
2017-04-04 16:33:22 +00:00
2022-05-10 15:51:11 +00:00
### Path Parameters
2017-04-04 16:33:22 +00:00
- `uuid` `(string: <required>)` - Specifies the UUID of the session to read.
2022-05-10 15:51:11 +00:00
### Query Parameters
- `dc` `(string: "")` - Specifies the datacenter to query.
This defaults to the datacenter of the agent being queried.
Using this parameter across datacenters is not recommended.
2020-04-06 20:27:35 +00:00
2020-04-23 22:13:18 +00:00
- `ns` `(string: "")` <EnterpriseAlert inline /> - Specifies the namespace to query.
2022-05-10 15:51:11 +00:00
You can also [specify the namespace through other methods](#methods-to-specify-namespace).
2017-04-04 16:33:22 +00:00
### Sample Request
2020-05-19 18:32:38 +00:00
```shell-session
2017-04-04 16:33:22 +00:00
$ curl \
2018-08-28 16:07:15 +00:00
http://127.0.0.1:8500/v1/session/info/adf4238a-882b-9ddc-4a9d-5b6758e4159e
2017-04-04 16:33:22 +00:00
```
### Sample Response
```json
[
{
2018-10-24 14:33:25 +00:00
"ID": "adf4238a-882b-9ddc-4a9d-5b6758e4159e",
"Name": "test-session",
2020-04-06 20:27:35 +00:00
"Node": "raja-laptop-02",
2021-07-20 06:52:21 +00:00
"LockDelay": 15000000000,
2018-10-24 14:33:25 +00:00
"Behavior": "release",
"TTL": "30s",
2021-07-20 06:52:21 +00:00
"NodeChecks": [
"serfHealth"
],
"ServiceChecks": null,
2018-10-24 14:33:25 +00:00
"CreateIndex": 1086449,
"ModifyIndex": 1086449
2017-04-04 16:33:22 +00:00
}
]
```
2020-02-11 10:03:28 +00:00
If the session does not exist, an empty JSON list `[]` is returned.
2017-04-04 16:33:22 +00:00
## List Sessions for Node
This endpoint returns the active sessions for a given node.
2022-03-19 20:32:38 +00:00
@include 'http_api_results_filtered_by_acls.mdx'
2020-04-06 20:27:35 +00:00
| Method | Path | Produces |
| :----- | :-------------------- | ------------------ |
| `GET` | `/session/node/:node` | `application/json` |
2017-04-04 16:33:22 +00:00
The table below shows this endpoint's support for
2023-01-25 16:52:43 +00:00
[blocking queries](/consul/api-docs/features/blocking),
[consistency modes](/consul/api-docs/features/consistency),
[agent caching](/consul/api-docs/features/caching), and
[required ACLs](/consul/api-docs/api-structure#authentication).
2017-04-04 16:33:22 +00:00
2018-09-06 10:34:28 +00:00
| Blocking Queries | Consistency Modes | Agent Caching | ACL Required |
| ---------------- | ----------------- | ------------- | -------------- |
| `YES` | `all` | `none` | `session:read` |
2017-04-04 16:33:22 +00:00
2022-05-10 15:51:11 +00:00
### Path Parameters
2017-04-04 16:33:22 +00:00
- `node` `(string: <required>)` - Specifies the name or ID of the node to query.
2022-05-10 15:51:11 +00:00
### Query Parameters
2017-04-04 16:33:22 +00:00
- `dc` `(string: "")` - Specifies the datacenter to query. This will default to
2022-05-10 15:51:11 +00:00
the datacenter of the agent being queried.
Using this parameter across datacenters is not recommended.
2020-04-06 20:27:35 +00:00
2020-04-23 22:13:18 +00:00
- `ns` `(string: "")` <EnterpriseAlert inline /> - Specifies the namespace to query.
2022-05-10 15:51:11 +00:00
You can also [specify the namespace through other methods](#methods-to-specify-namespace).
2020-04-06 20:27:35 +00:00
The namespace may be specified as '\*' and then results will be returned for all namespaces.
2017-04-04 16:33:22 +00:00
### Sample Request
2020-05-19 18:32:38 +00:00
```shell-session
2017-04-04 16:33:22 +00:00
$ curl \
2018-08-28 16:07:15 +00:00
http://127.0.0.1:8500/v1/session/node/node-abcd1234
2017-04-04 16:33:22 +00:00
```
### Sample Response
```json
[
{
2018-10-24 14:33:25 +00:00
"ID": "adf4238a-882b-9ddc-4a9d-5b6758e4159e",
"Name": "test-session",
2021-07-20 06:52:21 +00:00
"Node": "node-abcd1234",
"LockDelay": 15000000000,
2018-10-24 14:33:25 +00:00
"Behavior": "release",
"TTL": "30s",
2021-07-20 06:52:21 +00:00
"NodeChecks": [
"serfHealth"
],
"ServiceChecks": null,
2018-10-24 14:33:25 +00:00
"CreateIndex": 1086449,
"ModifyIndex": 1086449
}
2017-04-04 16:33:22 +00:00
]
```
## List Sessions
This endpoint returns the list of active sessions.
2022-03-19 20:32:38 +00:00
@include 'http_api_results_filtered_by_acls.mdx'
2020-04-06 20:27:35 +00:00
| Method | Path | Produces |
| :----- | :-------------- | ------------------ |
| `GET` | `/session/list` | `application/json` |
2017-04-04 16:33:22 +00:00
The table below shows this endpoint's support for
2023-01-25 16:52:43 +00:00
[blocking queries](/consul/api-docs/features/blocking),
[consistency modes](/consul/api-docs/features/consistency),
[agent caching](/consul/api-docs/features/caching), and
[required ACLs](/consul/api-docs/api-structure#authentication).
2017-04-04 16:33:22 +00:00
2018-09-06 10:34:28 +00:00
| Blocking Queries | Consistency Modes | Agent Caching | ACL Required |
| ---------------- | ----------------- | ------------- | -------------- |
| `YES` | `all` | `none` | `session:read` |
2017-04-04 16:33:22 +00:00
2022-05-10 15:51:11 +00:00
### Query Parameters
2017-04-04 16:33:22 +00:00
- `dc` `(string: "")` - Specifies the datacenter to query. This will default to
2022-05-10 15:51:11 +00:00
the datacenter of the agent being queried.
Using this parameter across datacenters is not recommended.
2020-04-06 20:27:35 +00:00
2020-04-23 22:13:18 +00:00
- `ns` `(string: "")` <EnterpriseAlert inline /> - Specifies the namespace to query.
2022-05-10 15:51:11 +00:00
You can also [specify the namespace through other methods](#methods-to-specify-namespace).
2020-04-06 20:27:35 +00:00
The namespace may be specified as '\*' and then results will be returned for all namespaces.
2017-04-04 16:33:22 +00:00
### Sample Request
2020-05-19 18:32:38 +00:00
```shell-session
2017-04-04 16:33:22 +00:00
$ curl \
2018-08-28 16:07:15 +00:00
http://127.0.0.1:8500/v1/session/list
2017-04-04 16:33:22 +00:00
```
### Sample Response
```json
[
{
2018-10-24 14:33:25 +00:00
"ID": "adf4238a-882b-9ddc-4a9d-5b6758e4159e",
"Name": "test-session",
2020-04-06 20:27:35 +00:00
"Node": "raja-laptop-02",
2021-07-20 06:52:21 +00:00
"LockDelay": 15000000000,
2018-10-24 14:33:25 +00:00
"Behavior": "release",
"TTL": "30s",
2021-07-20 06:52:21 +00:00
"NodeChecks": [
"serfHealth"
],
"ServiceChecks": null,
2018-10-24 14:33:25 +00:00
"CreateIndex": 1086449,
"ModifyIndex": 1086449
}
2017-04-04 16:33:22 +00:00
]
```
## Renew Session
This endpoint renews the given session. This is used with sessions that have a
TTL, and it extends the expiration by the TTL.
2020-04-06 20:27:35 +00:00
| Method | Path | Produces |
| :----- | :--------------------- | ------------------ |
| `PUT` | `/session/renew/:uuid` | `application/json` |
2017-04-04 16:33:22 +00:00
The table below shows this endpoint's support for
2023-01-25 16:52:43 +00:00
[blocking queries](/consul/api-docs/features/blocking),
[consistency modes](/consul/api-docs/features/consistency),
[agent caching](/consul/api-docs/features/caching), and
[required ACLs](/consul/api-docs/api-structure#authentication).
2017-04-04 16:33:22 +00:00
2018-09-06 10:34:28 +00:00
| Blocking Queries | Consistency Modes | Agent Caching | ACL Required |
| ---------------- | ----------------- | ------------- | --------------- |
| `NO` | `none` | `none` | `session:write` |
2017-04-04 16:33:22 +00:00
2022-05-10 15:51:11 +00:00
### Path Parameters
2017-04-04 16:33:22 +00:00
- `uuid` `(string: <required>)` - Specifies the UUID of the session to renew.
2022-05-10 15:51:11 +00:00
### Query Parameters
2017-04-04 16:33:22 +00:00
- `dc` `(string: "")` - Specifies the datacenter to query. This will default to
2022-05-10 15:51:11 +00:00
the datacenter of the agent being queried.
Using this parameter across datacenters is not recommended.
2020-04-06 20:27:35 +00:00
2020-04-23 22:13:18 +00:00
- `ns` `(string: "")` <EnterpriseAlert inline /> - Specifies the namespace to query.
2022-05-10 15:51:11 +00:00
You can also [specify the namespace through other methods](#methods-to-specify-namespace).
2017-04-04 16:33:22 +00:00
### Sample Request
2020-05-19 18:32:38 +00:00
```shell-session
2017-04-04 16:33:22 +00:00
$ curl \
--request PUT \
2018-08-28 16:07:15 +00:00
http://127.0.0.1:8500/v1/session/renew/adf4238a-882b-9ddc-4a9d-5b6758e4159e
2017-04-04 16:33:22 +00:00
```
### Sample Response
```json
[
{
2018-10-24 14:33:25 +00:00
"ID": "adf4238a-882b-9ddc-4a9d-5b6758e4159e",
"Name": "test-session",
2020-04-06 20:27:35 +00:00
"Node": "raja-laptop-02",
2021-07-20 06:52:21 +00:00
"LockDelay": 15000000000,
2017-04-04 16:33:22 +00:00
"Behavior": "release",
2021-07-20 06:52:21 +00:00
"TTL": "30s",
"NodeChecks": [
"serfHealth"
],
"ServiceChecks": null,
2018-10-24 14:33:25 +00:00
"CreateIndex": 1086449,
"ModifyIndex": 1086449
2017-04-04 16:33:22 +00:00
}
]
```
-> **Note:** Consul may return a TTL value higher than the one specified during session creation. This indicates the server is under high load and is requesting clients renew less often.
2022-05-10 15:51:11 +00:00
2023-10-23 11:41:24 +00:00
## Methods to specify namespace <EnterpriseAlert inline />
2022-05-10 15:51:11 +00:00
Session endpoints
support several methods for specifying the namespace of session resources
with the following order of precedence:
1. `ns` query parameter
1. `X-Consul-Namespace` request header
1. Namespace is inherited from the namespace of the request's ACL token (if any)
1. The `default` namespace