The consulate.Consul class is core interface for interacting with all parts of the Consul API.

Usage Examples

Here is an example where the initial consulate.Consul is created, connecting to Consul at localhost on port 8500. Once connected, a list of all service checks is returned.

import consulate

# Create a new instance of a consulate session
session = consulate.Consul()

# Get all of the service checks for the local agent
checks = session.agent.checks()

This next example creates a new Consul passing in an authorization token and then sets a key in the Consul KV service:

import consulate

session = consulate.Consul(token='5d24c96b4f6a4aefb99602ce9b60d16b')

# Set the key named release_flag to True
session.kv['release_flag'] = True


class consulate.Consul(host='localhost', port=8500, datacenter=None, token=None, scheme='http', adapter=None, verify=True, cert=None)

Access the Consul HTTP API via Python.

The default values connect to Consul via localhost:8500 via http. If you want to connect to Consul via a local UNIX socket, you’ll need to override both the scheme, port and the adapter like so:

consul = consulate.Consul('/path/to/socket', None, scheme='http+unix',
services =
  • host (str) – The host name to connect to (Default: localhost)
  • port (int) – The port to connect on (Default: 8500)
  • datacenter (str) – Specify a specific data center
  • token (str) – Specify a ACL token to use
  • scheme (str) – Specify the scheme (Default: http)
  • adapter (class) – Specify to override the request adapter (Default: consulate.adapters.Request)
  • verify (bool/str) – Specify how to verify TLS certificates
  • cert (tuple) – Specify client TLS certificate and key files

Access the Consul ACL API

Return type:consulate.api.acl.ACL

Access the Consul Agent API

Return type:consulate.api.agent.Agent

Access the Consul Catalog API

Return type:consulate.api.catalog.Catalog

Access the Consul Events API

Return type:consulate.api.event.Event

Access the Consul Health API


Access the Consul KV API

Return type:consulate.api.kv.KV

Wrapper for easy KV locks.


import consulate

consul = consulate.Consul()
with consul.lock.acquire('my-key'):
    print('Locked: {}'.format(consul.lock.key))
    # Do stuff
Return type:Lock

Access the Consul Session API

Return type:consulate.api.session.Session

Access the Consul Status API

Return type:consulate.api.status.Status