etcd v3 uses gRPC for its messaging protocol. The etcd project includes a gRPC-based Go client and a command line utility, etcdctl, for communicating with an etcd cluster through gRPC. For languages with no gRPC support, etcd provides a JSON grpc-gateway. This gateway serves a RESTful proxy that translates HTTP/JSON requests into gRPC messages.

Using grpc-gateway

The gateway accepts a JSON mapping for etcd’s protocol buffer message definitions. Note that key and value fields are defined as byte arrays and therefore must be base64 encoded in JSON.

Use curl to put and get a key:

  1. <<COMMENT
  2. https://www.base64encode.org/
  3. foo is 'Zm9v' in Base64
  4. bar is 'YmFy'
  5. COMMENT
  6. curl -L http://localhost:2379/v3alpha/kv/put \
  7. -X POST -d '{"key": "Zm9v", "value": "YmFy"}'
  8. # {"header":{"cluster_id":"12585971608760269493","member_id":"13847567121247652255","revision":"2","raft_term":"3"}}
  9. curl -L http://localhost:2379/v3alpha/kv/range \
  10. -X POST -d '{"key": "Zm9v"}'
  11. # {"header":{"cluster_id":"12585971608760269493","member_id":"13847567121247652255","revision":"2","raft_term":"3"},"kvs":[{"key":"Zm9v","create_revision":"2","mod_revision":"2","version":"1","value":"YmFy"}],"count":"1"}
  12. # get all keys prefixed with "foo"
  13. curl -L http://localhost:2379/v3alpha/kv/range \
  14. -X POST -d '{"key": "Zm9v", "range_end": "Zm9w"}'
  15. # {"header":{"cluster_id":"12585971608760269493","member_id":"13847567121247652255","revision":"2","raft_term":"3"},"kvs":[{"key":"Zm9v","create_revision":"2","mod_revision":"2","version":"1","value":"YmFy"}],"count":"1"}

Use curl to watch a key:

  1. curl http://localhost:2379/v3alpha/watch \
  2. -X POST -d '{"create_request": {"key":"Zm9v"} }' &
  3. # {"result":{"header":{"cluster_id":"12585971608760269493","member_id":"13847567121247652255","revision":"1","raft_term":"2"},"created":true}}
  4. curl -L http://localhost:2379/v3alpha/kv/put \
  5. -X POST -d '{"key": "Zm9v", "value": "YmFy"}' >/dev/null 2>&1
  6. # {"result":{"header":{"cluster_id":"12585971608760269493","member_id":"13847567121247652255","revision":"2","raft_term":"2"},"events":[{"kv":{"key":"Zm9v","create_revision":"2","mod_revision":"2","version":"1","value":"YmFy"}}]}}

Use curl to issue a transaction:

  1. curl -L http://localhost:2379/v3alpha/kv/txn \
  2. -X POST \
  3. -d '{"compare":[{"target":"CREATE","key":"Zm9v","createRevision":"2"}],"success":[{"requestPut":{"key":"Zm9v","value":"YmFy"}}]}'
  4. # {"header":{"cluster_id":"12585971608760269493","member_id":"13847567121247652255","revision":"3","raft_term":"2"},"succeeded":true,"responses":[{"response_put":{"header":{"revision":"3"}}}]}

Swagger

Generated Swagger API definitions can be found at rpc.swagger.json.