github.com/outbrain/consul@v1.4.5/website/source/api/txn.html.md (about) 1 --- 2 layout: api 3 page_title: Transaction - HTTP API 4 sidebar_current: api-txn 5 description: |- 6 The /txn endpoint manages multiple operations in Consul, including catalog updates and fetches of multiple KV entries inside a single, atomic transaction. 7 --- 8 9 # Transactions HTTP API 10 11 The `/txn` endpoint manages multiple operations in Consul, including catalog 12 updates and fetches of multiple KV entries inside a single, atomic 13 transaction. 14 15 ## Create Transaction 16 17 This endpoint permits submitting a list of operations to apply to Consul 18 inside of a transaction. If any operation fails, the transaction is rolled back 19 and none of the changes are applied. 20 21 If the transaction does not contain any write operations then it will be 22 fast-pathed internally to an endpoint that works like other reads, except that 23 blocking queries are not currently supported. In this mode, you may supply the 24 `?stale` or `?consistent` query parameters with the request to control 25 consistency. To support bounding the acceptable staleness of data, read-only 26 transaction responses provide the `X-Consul-LastContact` header containing the 27 time in milliseconds that a server was last contacted by the leader node. The 28 `X-Consul-KnownLeader` header also indicates if there is a known leader. These 29 won't be present if the transaction contains any write operations, and any 30 consistency query parameters will be ignored, since writes are always managed by 31 the leader via the Raft consensus protocol. 32 33 | Method | Path | Produces | 34 | ------ | ---------------------------- | -------------------------- | 35 | `PUT` | `/txn` | `application/json` | 36 37 The table below shows this endpoint's support for 38 [blocking queries](/api/index.html#blocking-queries), 39 [consistency modes](/api/index.html#consistency-modes), 40 [agent caching](/api/index.html#agent-caching), and 41 [required ACLs](/api/index.html#acls). 42 43 | Blocking Queries | Consistency Modes | Agent Caching | ACL Required | 44 | ---------------- | ----------------- | ------------- | ------------ | 45 | `NO` | `all`<sup>1</sup> | `none` | `key:read,key:write`<br>`node:read,node:write`<br>`service:read,service:write`<sup>2</sup> 46 47 <sup>1</sup> For read-only transactions 48 <br> 49 <sup>2</sup> The ACL required depends on the operations in the transaction. 50 51 ### Parameters 52 53 - `dc` `(string: "")` - Specifies the datacenter to query. This will default 54 to the datacenter of the agent being queried. This is specified as part of the 55 URL as a query parameter. 56 57 - `KV` operations have the following fields: 58 59 - `Verb` `(string: <required>)` - Specifies the type of operation to perform. 60 Please see the table below for available verbs. 61 62 - `Key` `(string: <required>)` - Specifies the full path of the entry. 63 64 - `Value` `(string: "")` - Specifies a **base64-encoded** blob of data. Values 65 cannot be larger than 512kB. 66 67 - `Flags` `(int: 0)` - Specifies an opaque unsigned integer that can be 68 attached to each entry. Clients can choose to use this however makes sense 69 for their application. 70 71 - `Index` `(int: 0)` - Specifies an index. See the table below for more 72 information. 73 74 - `Session` `(string: "")` - Specifies a session. See the table below for more 75 information. 76 77 - `Node` operations have the following fields: 78 79 - `Verb` `(string: <required>)` - Specifies the type of operation to perform. 80 81 - `Node` `(Node: <required>)` - Specifies the node information to use 82 for the operation. See the [catalog endpoint](/api/catalog.html#parameters) 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. 83 84 - `Service` operations have the following fields: 85 86 - `Verb` `(string: <required>)` - Specifies the type of operation to perform. 87 88 - `Node` `(string: <required>)` = Specifies the name of the node to use for 89 this service operation. 90 91 - `Service` `(Service: <required>)` - Specifies the service instance information to use 92 for the operation. See the [catalog endpoint](/api/catalog.html#parameters) for the fields in this object. 93 94 - `Check` operations have the following fields: 95 96 - `Verb` `(string: <required>)` - Specifies the type of operation to perform. 97 98 - `Service` `(Service: <required>)` - Specifies the check to use 99 for the operation. See the [catalog endpoint](/api/catalog.html#parameters) for the fields in this object. 100 101 Please see the table below for available verbs. 102 ### Sample Payload 103 104 The body of the request should be a list of operations to perform inside the 105 atomic transaction. Up to 64 operations may be present in a single transaction. 106 107 ```javascript 108 [ 109 { 110 "KV": { 111 "Verb": "<verb>", 112 "Key": "<key>", 113 "Value": "<Base64-encoded blob of data>", 114 "Flags": <flags>, 115 "Index": <index>, 116 "Session": "<session id>" 117 } 118 }, 119 { 120 "Node": { 121 "Verb": "set", 122 "Node": { 123 "ID": "67539c9d-b948-ba67-edd4-d07a676d6673", 124 "Node": "bar", 125 "Address": "192.168.0.1", 126 "Datacenter": "dc1", 127 "Meta": { 128 "instance_type": "m2.large" 129 } 130 } 131 } 132 }, 133 { 134 "Service": { 135 "Verb": "delete", 136 "Node": "foo", 137 "Service": { 138 "ID": "db1" 139 } 140 } 141 }, 142 { 143 "Check": { 144 "Verb": "cas", 145 "Check": { 146 "Node": "bar", 147 "CheckID": "service:web1", 148 "Name": "Web HTTP Check", 149 "Status": "critical", 150 "ServiceID": "web1", 151 "ServiceName": "web", 152 "ServiceTags": null, 153 "Definition": { 154 "HTTP": "http://localhost:8080", 155 "Interval": "10s" 156 }, 157 "ModifyIndex": 22 158 } 159 } 160 } 161 ] 162 ``` 163 164 ### Sample Request 165 166 ```text 167 $ curl \ 168 --request PUT \ 169 --data @payload.json \ 170 http://127.0.0.1:8500/v1/txn 171 ``` 172 173 ### Sample Response 174 175 If the transaction can be processed, a status code of 200 will be returned if it 176 was successfully applied, or a status code of 409 will be returned if it was 177 rolled back. If either of these status codes are returned, the response will 178 look like this: 179 180 ```javascript 181 { 182 "Results": [ 183 { 184 "KV": { 185 "LockIndex": <lock index>, 186 "Key": "<key>", 187 "Flags": <flags>, 188 "Value": "<Base64-encoded blob of data, or null>", 189 "CreateIndex": <index>, 190 "ModifyIndex": <index> 191 } 192 }, 193 { 194 "Node": { 195 "ID": "67539c9d-b948-ba67-edd4-d07a676d6673", 196 "Node": "bar", 197 "Address": "192.168.0.1", 198 "Datacenter": "dc1", 199 "TaggedAddresses": null, 200 "Meta": { 201 "instance_type": "m2.large" 202 }, 203 "CreateIndex": 32, 204 "ModifyIndex": 32 205 } 206 }, 207 { 208 "Check": { 209 "Node": "bar", 210 "CheckID": "service:web1", 211 "Name": "Web HTTP Check", 212 "Status": "critical", 213 "Notes": "", 214 "Output": "", 215 "ServiceID": "web1", 216 "ServiceName": "web", 217 "ServiceTags": null, 218 "Definition": { 219 "HTTP": "http://localhost:8080", 220 "Interval": "10s" 221 }, 222 "CreateIndex": 22, 223 "ModifyIndex": 35 224 } 225 } 226 ], 227 "Errors": [ 228 { 229 "OpIndex": <index of failed operation>, 230 "What": "<error message for failed operation>" 231 }, 232 ... 233 ] 234 } 235 ``` 236 237 - `Results` has entries for some operations if the transaction was successful. 238 To save space, the `Value` for KV results will be `null` for any `Verb` other than "get" or 239 "get-tree". Like the `/v1/kv/<key>` endpoint, `Value` will be Base64-encoded 240 if it is present. Also, no result entries will be added for verbs that delete 241 keys. 242 243 - `Errors` has entries describing which operations failed if the transaction was 244 rolled back. The `OpIndex` gives the index of the failed operation in the 245 transaction, and `What` is a string with an error message about why that 246 operation failed. 247 248 ### Tables of Operations 249 250 #### KV Operations 251 252 The following tables summarize the available verbs and the fields that apply to 253 those operations ("X" means a field is required and "O" means it is optional): 254 255 | Verb | Operation | Key | Value | Flags | Index | Session | 256 | ------------------ | -------------------------------------------- | :--: | :---: | :---: | :---: | :-----: | 257 | `set` | Sets the `Key` to the given `Value` | `x` | `x` | `o` | | | 258 | `cas` | Sets, but with CAS semantics | `x` | `x` | `o` | `x` | | 259 | `lock` | Lock with the given `Session` | `x` | `x` | `o` | | `x` | 260 | `unlock` | Unlock with the given `Session` | `x` | `x` | `o` | | `x` | 261 | `get` | Get the key, fails if it does not exist | `x` | | | | | 262 | `get-tree` | Gets all keys with the prefix | `x` | | | | | 263 | `check-index` | Fail if modify index != index | `x` | | | `x` | | 264 | `check-session` | Fail if not locked by session | `x` | | | | `x` | 265 | `check-not-exists` | Fail if key exists | `x` | | | | | 266 | `delete` | Delete the key | `x` | | | | | 267 | `delete-tree` | Delete all keys with a prefix | `x` | | | | | 268 | `delete-cas` | Delete, but with CAS semantics | `x` | | | `x` | | 269 270 #### Node Operations 271 272 Node operations act on an individual node and require either a Node ID or name, giving precedence 273 to the ID if both are set. Delete operations will not return a result on success. 274 275 | Verb | Operation | 276 | ------------------ | -------------------------------------------- | 277 | `set` | Sets the node to the given state | 278 | `cas` | Sets, but with CAS semantics using the given ModifyIndex | 279 | `get` | Get the node, fails if it does not exist | 280 | `delete` | Delete the node | 281 | `delete-cas` | Delete, but with CAS semantics | 282 283 #### Service Operations 284 285 Service operations act on an individual service instance on the given node name. Both a node name 286 and valid service name are required. Delete operations will not return a result on success. 287 288 | Verb | Operation | 289 | ------------------ | -------------------------------------------- | 290 | `set` | Sets the service to the given state | 291 | `cas` | Sets, but with CAS semantics using the given ModifyIndex | 292 | `get` | Get the service, fails if it does not exist | 293 | `delete` | Delete the service | 294 | `delete-cas` | Delete, but with CAS semantics | 295 296 #### Check Operations 297 298 Check operations act on an individual health check instance on the given node name. Both a node name 299 and valid check ID are required. Delete operations will not return a result on success. 300 301 | Verb | Operation | 302 | ------------------ | -------------------------------------------- | 303 | `set` | Sets the health check to the given state | 304 | `cas` | Sets, but with CAS semantics using the given ModifyIndex | 305 | `get` | Get the check, fails if it does not exist | 306 | `delete` | Delete the check | 307 | `delete-cas` | Delete, but with CAS semantics |