github.com/outbrain/consul@v1.4.5/website/source/api/index.html.md (about) 1 --- 2 layout: api 3 page_title: HTTP API 4 sidebar_current: api-overview 5 description: |- 6 Consul exposes a RESTful HTTP API to control almost every aspect of the 7 Consul agent. 8 --- 9 10 # HTTP API 11 12 The main interface to Consul is a RESTful HTTP API. The API can perform basic 13 CRUD operations on nodes, services, checks, configuration, and more. 14 15 ## Version Prefix 16 17 All API routes are prefixed with `/v1/`. This documentation is only for the v1 API. 18 19 ## ACLs 20 21 Several endpoints in Consul use or require ACL tokens to operate. An agent 22 can be configured to use a default token in requests using the `acl_token` 23 configuration option. However, the token can also be specified per-request 24 by using the `X-Consul-Token` request header or Bearer header in Authorization 25 header or the `token` query string parameter. The request header takes 26 precedence over the default token, and the query string parameter takes 27 precedence over everything. 28 29 For more details about ACLs, please see the [ACL Guide](/docs/guides/acl.html). 30 31 ## Authentication 32 33 When authentication is enabled, a Consul token should be provided to API 34 requests using the `X-Consul-Token` header. This reduces the probability of the 35 token accidentally getting logged or exposed. When using authentication, 36 clients should communicate via TLS. 37 38 Here is an example using `curl`: 39 40 ```text 41 $ curl \ 42 --header "X-Consul-Token: abcd1234" \ 43 http://127.0.0.1:8500/v1/agent/members 44 ``` 45 46 Previously this was provided via a `?token=` query parameter. This functionality 47 exists on many endpoints for backwards compatibility, but its use is **highly 48 discouraged**, since it can show up in access logs as part of the URL. 49 50 ## Blocking Queries 51 52 Many endpoints in Consul support a feature known as "blocking queries". A 53 blocking query is used to wait for a potential change using long polling. Not 54 all endpoints support blocking, but each endpoint uniquely documents its support 55 for blocking queries in the documentation. 56 57 Endpoints that support blocking queries return an HTTP header named 58 `X-Consul-Index`. This is a unique identifier representing the current state of 59 the requested resource. 60 61 On subsequent requests for this resource, the client can set the `index` query 62 string parameter to the value of `X-Consul-Index`, indicating that the client 63 wishes to wait for any changes subsequent to that index. 64 65 When this is provided, the HTTP request will "hang" until a change in the system 66 occurs, or the maximum timeout is reached. A critical note is that the return of 67 a blocking request is **no guarantee** of a change. It is possible that the 68 timeout was reached or that there was an idempotent write that does not affect 69 the result of the query. 70 71 In addition to `index`, endpoints that support blocking will also honor a `wait` 72 parameter specifying a maximum duration for the blocking request. This is 73 limited to 10 minutes. If not set, the wait time defaults to 5 minutes. This 74 value can be specified in the form of "10s" or "5m" (i.e., 10 seconds or 5 75 minutes, respectively). A small random amount of additional wait time is added 76 to the supplied maximum `wait` time to spread out the wake up time of any 77 concurrent requests. This adds up to `wait / 16` additional time to the maximum 78 duration. 79 80 ### Implementation Details 81 82 While the mechanism is relatively simple to work with, there are a few edge 83 cases that must be handled correctly. 84 85 * **Reset the index if it goes backwards**. While indexes in general are 86 monotonically increasing(i.e. they should only ever increase as time passes), 87 there are several real-world scenarios in 88 which they can go backwards for a given query. Implementations must check 89 to see if a returned index is lower than the previous value, 90 and if it is, should reset index to `0` - effectively restarting their blocking loop. 91 Failure to do so may cause the client to miss future updates for an unbounded 92 time, or to use an invalid index value that causes no blocking and increases 93 load on the servers. Cases where this can occur include: 94 * If a raft snapshot is restored on the servers with older version of the data. 95 * KV list operations where an item with the highest index is removed. 96 * A Consul upgrade changes the way watches work to optimize them with more 97 granular indexes. 98 99 * **Sanity check index is greater than zero**. After the initial request (or a 100 reset as above) the `X-Consul-Index` returned _should_ always be greater than zero. It 101 is a bug in Consul if it is not, however this has happened a few times and can 102 still be triggered on some older Consul versions. It's especially bad because it 103 causes blocking clients that are not aware to enter a busy loop, using excessive 104 client CPU and causing high load on servers. It is _always_ safe to use an 105 index of `1` to wait for updates when the data being requested doesn't exist 106 yet, so clients _should_ sanity check that their index is at least 1 after 107 each blocking response is handled to be sure they actually block on the next 108 request. 109 110 * **Rate limit**. The blocking query mechanism is reasonably efficient when updates 111 are relatively rare (order of tens of seconds to minutes between updates). In cases 112 where a result gets updated very fast however - possibly during an outage or incident 113 with a badly behaved client - blocking query loops degrade into busy loops that 114 consume excessive client CPU and cause high server load. While it's possible to just add a sleep 115 to every iteration of the loop, this is **not** recommended since it causes update 116 delivery to be delayed in the happy case, and it can exacerbate the problem since 117 it increases the chance that the index has changed on the next request. Clients 118 _should_ instead rate limit the loop so that in the happy case they proceed without 119 waiting, but when values start to churn quickly they degrade into polling at a 120 reasonable rate (say every 15 seconds). Ideally this is done with an algorithm that 121 allows a couple of quick successive deliveries before it starts to limit rate - a 122 [token bucket](https://en.wikipedia.org/wiki/Token_bucket) with burst of 2 is a simple 123 way to achieve this. 124 125 ### Hash-based Blocking Queries 126 127 A limited number of agent endpoints also support blocking however because the 128 state is local to the agent and not managed with a consistent raft index, their 129 blocking mechanism is different. 130 131 Since there is no monotonically increasing index, each response instead contains 132 a header `X-Consul-ContentHash` which is an opaque hash digest generated by 133 hashing over all fields in the response that are relevant. 134 135 Subsequent requests may be sent with a query parameter `hash=<value>` where 136 `value` is the last hash header value seen, and this will block until the `wait` 137 timeout is passed or until the local agent's state changes in such a way that 138 the hash would be different. 139 140 Other than the different header and query parameter names, the biggest 141 difference is that hash values are opaque and can't be compared to see if one 142 result is older or newer than another. In general hash-based blocking will not 143 return too early due to an idempotent update since the hash will remain the same 144 unless the result actually changes, however as with index-based blocking there 145 is no strict guarantee that clients will never observe the same result delivered 146 before the full timeout has elapsed. 147 148 ## Consistency Modes 149 150 Most of the read query endpoints support multiple levels of consistency. Since 151 no policy will suit all clients' needs, these consistency modes allow the user 152 to have the ultimate say in how to balance the trade-offs inherent in a 153 distributed system. 154 155 The three read modes are: 156 157 - `default` - If not specified, the default is strongly consistent in almost all 158 cases. However, there is a small window in which a new leader may be elected 159 during which the old leader may service stale values. The trade-off is fast 160 reads but potentially stale values. The condition resulting in stale reads is 161 hard to trigger, and most clients should not need to worry about this case. 162 Also, note that this race condition only applies to reads, not writes. 163 164 - `consistent` - This mode is strongly consistent without caveats. It requires 165 that a leader verify with a quorum of peers that it is still leader. This 166 introduces an additional round-trip to all server nodes. The trade-off is 167 increased latency due to an extra round trip. Most clients should not use this 168 unless they cannot tolerate a stale read. 169 170 - `stale` - This mode allows any server to service the read regardless of 171 whether it is the leader. This means reads can be arbitrarily stale; however, 172 results are generally consistent to within 50 milliseconds of the leader. The 173 trade-off is very fast and scalable reads with a higher likelihood of stale 174 values. Since this mode allows reads without a leader, a cluster that is 175 unavailable will still be able to respond to queries. 176 177 To switch these modes, either the `stale` or `consistent` query parameters 178 should be provided on requests. It is an error to provide both. 179 180 Note that some endpoints support a `cached` parameter which has some of the same 181 semantics as `stale` but different trade offs. This behaviour is described in 182 [Agent Caching](#agent-caching). 183 184 To support bounding the acceptable staleness of data, responses provide the 185 `X-Consul-LastContact` header containing the time in milliseconds that a server 186 was last contacted by the leader node. The `X-Consul-KnownLeader` header also 187 indicates if there is a known leader. These can be used by clients to gauge the 188 staleness of a result and take appropriate action. 189 190 ## Agent Caching 191 192 Some read endpoints support agent caching. They are clearly marked in the 193 documentation. Agent caching can take two forms, [`simple`](#simple-caching) or 194 [`background refresh`](#blocking-refresh-caching) depending on the endpoint's 195 semantics. The documentation for each endpoint clearly identify which if any 196 form of caching is supported. The details for each are described below. 197 198 Where supported, caching can be enabled though the `?cached` parameter. 199 Combining `?cached` with `?consistent` is an error. 200 201 ### Simple Caching 202 203 Endpoints supporting simple caching may return a result directly from the local 204 agent's cache without a round trip to the servers. By default the agent caches 205 results for a relatively long time (3 days) such that it can still return a 206 result even if the servers are unavailable for an extended period to enable 207 "fail static" semantics. 208 209 That means that with no other arguments, `?cached` queries might receive a 210 response which is days old. To request better freshness, the HTTP 211 `Cache-Control` header may be set with a directive like `max-age=<seconds>`. In 212 this case the agent will attempt to re-fetch the result from the servers if the 213 cached value is older than the given `max-age`. If the servers can't be reached 214 a 500 is returned as normal. 215 216 To allow clients to maintain fresh results in normal operation but allow stale 217 ones if the servers are unavailable, the `stale-if-error=<seconds>` directive 218 may be additionally provided in the `Cache-Control` header. This will return the 219 cached value anyway even it it's older than `max-age` (provided it's not older 220 than `stale-if-error`) rather than a 500. It must be provided along with a 221 `max-age` or `must-revalidate`. The `Age` response header, if larger than 222 `max-age` can be used to determine if the server was unreachable and a cached 223 version returned instead. 224 225 For example, assuming there is a cached response that is 65 seconds old, and 226 that the servers are currently unavailable, `Cache-Control: max-age=30` will 227 result in a 500 error, while `Cache-Control: max-age=30 stale-if-error=259200` 228 will result in the cached response being returned. 229 230 A request setting either `max-age=0` or `must-revalidate` directives will cause 231 the agent to always re-fetch the response from servers. Either can be combined 232 with `stale-if-error=<seconds>` to ensure fresh results when the servers are 233 available, but falling back to cached results if the request to the servers 234 fails. 235 236 Requests that do not use `?cached` currently bypass the cache entirely so the 237 cached response returned might be more stale than the last uncached response 238 returned on the same agent. If this causes problems, it is possible to make 239 requests using `?cached` and setting `Cache-Control: must-revalidate` to have 240 always-fresh results yet keeping the cache populated with the most recent 241 result. 242 243 In all cases the HTTP `X-Cache` header is always set in the response to either 244 `HIT` or `MISS` indicating whether the response was served from cache or not. 245 246 For cache hits, the HTTP `Age` header is always set in the response to indicate 247 how many seconds since that response was fetched from the servers. 248 249 ### Background Refresh Caching 250 251 Endpoints supporting background refresh caching may return a result directly 252 from the local agent's cache without a round trip to the severs. The first fetch 253 that is a miss will cause an initial fetch from the servers, but will also 254 trigger the agent to begin a background blocking query that watches for any 255 changes to that result and updates the cached value if changes occur. 256 257 Following requests will _always_ be a cache hit until there has been no request 258 for the resource for the TTL (which is typically 3 days). 259 260 Clients can perform blocking queries against the local agent which will be 261 served from the cache. This allows multiple clients to watch the same resource 262 locally while only a single blocking watch for that resource will be made to the 263 servers from a given client agent. 264 265 HTTP `Cache-Control` headers are ignored in this mode since the cache is being 266 actively updated and has different semantics to a typical passive cache. 267 268 In all cases the HTTP `X-Cache` header is always set in the response to either 269 `HIT` or `MISS` indicating whether the response was served from cache or not. 270 271 For cache hits, the HTTP `Age` header is always set in the response to indicate 272 how many seconds since that response was fetched from the servers. As long as 273 the local agent has an active connection to the servers, the age will always be 274 `0` since the value is up-to-date. If the agent get's disconnected, the cached 275 result is still returned but with an `Age` that indicates how many seconds have 276 elapsed since the local agent got disconnected from the servers, during which 277 time updates to the result might have been missed. 278 279 ## Formatted JSON Output 280 281 By default, the output of all HTTP API requests is minimized JSON. If the client 282 passes `pretty` on the query string, formatted JSON will be returned. 283 284 ## HTTP Methods 285 286 Consul's API aims to be RESTful, although there are some exceptions. The API 287 responds to the standard HTTP verbs GET, PUT, and DELETE. Each API method will 288 clearly document the verb(s) it responds to and the generated response. The same 289 path with different verbs may trigger different behavior. For example: 290 291 ```text 292 PUT /v1/kv/foo 293 GET /v1/kv/foo 294 ``` 295 296 Even though these share a path, the `PUT` operation creates a new key whereas 297 the `GET` operation reads an existing key. 298 299 Here is the same example using `curl`: 300 301 ```shell 302 $ curl \ 303 --request PUT \ 304 --data 'hello consul' \ 305 http://127.0.0.1:8500/v1/kv/foo 306 ``` 307 308 ## Translated Addresses 309 310 Consul 0.7 added the ability to translate addresses in HTTP response based on 311 the configuration setting for 312 [`translate_wan_addrs`](/docs/agent/options.html#translate_wan_addrs). In order 313 to allow clients to know if address translation is in effect, the 314 `X-Consul-Translate-Addresses` header will be added if translation is enabled, 315 and will have a value of `true`. If translation is not enabled then this header 316 will not be present. 317 318 ## UUID Format 319 320 UUID-format identifiers generated by the Consul API use the 321 [hashicorp/go-uuid](https://github.com/hashicorp/go-uuid) library. 322 323 These UUID-format strings are generated using high quality, purely random bytes. 324 It is not intended to be RFC compliant, merely to use a well-understood string 325 representation of a 128-bit value.