Transactions HTTP API
The /txn endpoint manages multiple operations in Consul, including catalog
updates and fetches of multiple KV entries inside a single, atomic
transaction.
Create Transaction
This endpoint permits submitting a list of operations to apply to Consul inside of a transaction. If any operation fails, the transaction is rolled back and none of the changes are applied.
If the transaction does not contain any write operations then it will be
fast-pathed internally to an endpoint that works like other reads, except that
blocking queries are not currently supported. In this mode, you may supply the
?stale or ?consistent query parameters with the request to control
consistency. To support bounding the acceptable staleness of data, read-only
transaction responses provide the X-Consul-LastContact header containing the
time in milliseconds that a server was last contacted by the leader node. The
X-Consul-KnownLeader header also indicates if there is a known leader. These
won't be present if the transaction contains any write operations, and any
consistency query parameters will be ignored, since writes are always managed by
the leader via the Raft consensus protocol.
| Method | Path | Produces |
|---|---|---|
PUT | /txn | application/json |
The table below shows this endpoint's support for blocking queries, consistency modes, agent caching, and required ACLs.
| Blocking Queries | Consistency Modes | Agent Caching | ACL Required |
|---|---|---|---|
NO | all1 | none | key:read,key:writenode:read,node:writeservice:read,service:write2 |
1 For read-only transactions
2 The ACL required depends on the operations in the transaction.
Query Parameters
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 URL as a query parameter.
JSON Request Body Schema
A JSON array of operations objects, each with
a key of the operation name (KV, Node, Service, or Check), and
a value of an object specific to that operation.
KVoperations have the following fields:Verb(string: <required>)- Specifies the type of operation to perform. Please see the table below for available verbs.Key(string: <required>)- Specifies the full path of the entry.Value(string: "")- Specifies a base64-encoded blob of data. Values cannot be larger than 512kB.Flags(int: 0)- Specifies an opaque unsigned integer that can be attached to each entry. Clients can choose to use this however makes sense for their application.Index(int: 0)- Specifies an index. See the table below for more information.Session(string: "")- Specifies a session. See the table below for more information.Namespace(string: "")- Specifies the namespace to create the KV data If not provided, the namespace will be inherited from the request's ACL token or will default to theEnterprisedefaultnamespace. Added in Consul 1.7.0.
Nodeoperations have the following fields:Verb(string: <required>)- Specifies the type of operation to perform.Node(Node: <required>)- Specifies the node information to use for the operation. See the catalog endpoint for the fields in this object. Note the only the node can be specified here, not any services or checks - separate service or check operations must be used for those.
Serviceoperations have the following fields:Verb(string: <required>)- Specifies the type of operation to perform.Node(string: <required>)= Specifies the name of the node to use for this service operation.Service(Service: <required>)- Specifies the service instance information to use for the operation. See the catalog endpoint for the fields in this object.
Checkoperations have the following fields:Verb(string: <required>)- Specifies the type of operation to perform.Check(Service: <required>)- Specifies the check to use for the operation. See the catalog endpoint for the fields in this object.
Please see the table below for available verbs.
Sample Payload
The body of the request should be a list of operations to perform inside the atomic transaction. Up to 64 operations may be present in a single transaction.
[
{
"KV": {
"Verb": "<verb>",
"Key": "<key>",
"Value": "<Base64-encoded blob of data>",
"Flags": <flags>,
"Index": <index>,
"Session": "<session id>"
}
},
{
"Node": {
"Verb": "set",
"Node": {
"ID": "67539c9d-b948-ba67-edd4-d07a676d6673",
"Node": "bar",
"Address": "192.168.0.1",
"Datacenter": "dc1",
"Meta": {
"instance_type": "m2.large"
}
}
}
},
{
"Service": {
"Verb": "delete",
"Node": "foo",
"Service": {
"ID": "db1"