> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Demo Using Tokens with the Consul API

> This article demonstrates how to use an ACL token to authenticate HTTP requests with the Consul Key/Value store.

Welcome to the final lab of this guide. Here you’ll leverage an existing bootstrap ACL token to authenticate HTTP requests against the Consul Key/Value (K/V) store. This demonstration covers:

1. Loading the token from a file
2. Creating or recreating an ACL policy
3. Retrieving K/V entries via `curl` and `jq`
4. Two authentication methods for the Consul API

<Callout icon="lightbulb" color="#1CB2FE">
  Before proceeding, ensure Consul is running and you have access to the `consul` binary. For more details, see the [Consul documentation](https://www.consul.io/docs).
</Callout>

## 1. Export the ACL Token

Set the `CONSUL_HTTP_TOKEN_FILE` environment variable to read your token from `token.txt`:

```bash theme={null}
export CONSUL_HTTP_TOKEN_FILE=token.txt
```

<Callout icon="triangle-alert" color="#FF6B6B">
  Keep your token file secure. Avoid committing it to version control or sharing it publicly.
</Callout>

## 2. Create (or Recreate) an ACL Policy

Use `consul acl policy create` to define a policy with the required rules. If the policy name already exists, choose a new one:

```bash theme={null}
consul acl policy create -name "test123" -rules @rules.hcl
# On error (name exists), try:
consul acl policy create -name "test456" -rules @rules.hcl
```

Sample output after creating `test456`:

```text theme={null}
ID:        51eff8b-4581-7009-2d44-78edf6f105da
Name:      test456
Namespace: default
Rules:
  node "web-server-01" {
    policy = "write"
  }
  key_prefix "apps/eCommerce" {
    policy = "write"
  }
  session_prefix "" {
    policy = "write"
  }
  service "eCommerce-Front-End" {
    policy = "write"
  }
```

## 3. Verify Your Token and List K/V Entries

Clear your terminal and display the token:

```bash theme={null}
clear
cat token.txt
# Example output:
# c7142d5a-aba1-78ba-f521-189971e29c24
```

Then list all keys in the K/V store:

```bash theme={null}
consul kv get -recurse
```

Expected output:

```text theme={null}
apps/eCommerce/database:billing
apps/eCommerce/database_host:customer_db
apps/eCommerce/environment:production
apps/eCommerce/version:4.5
apps/search/url:search.service.consul
apps/search/version:4
```

## 4. Authenticate API Requests

Now that you know the key (`apps/eCommerce/database_host`) and have your ACL token, you can fetch its value using the Consul HTTP API. Below are two supported methods:

| Header Type    | Description                  | Header Example                                |
| -------------- | ---------------------------- | --------------------------------------------- |
| X-Consul-Token | Consul-specific token header | `X-Consul-Token: c7142d5a-aba1-78ba-f521-...` |
| Authorization  | Standard HTTP Bearer token   | `Authorization: Bearer c7142d5a-aba1-78ba...` |

### Method 1: X-Consul-Token Header

```bash theme={null}
curl \
  --header "X-Consul-Token: c7142d5a-aba1-78ba-f521-189971e29c24" \
  http://127.0.0.1:8500/v1/kv/apps/eCommerce/database_host | jq
```

Response:

```json theme={null}
[
  {
    "LockIndex": 0,
    "Key": "apps/eCommerce/database_host",
    "Flags": 0,
    "Value": "Y3VzdG9tZXJzZGI=",
    "Namespace": "default",
    "CreateIndex": 2336,
    "ModifyIndex": 2336
  }
]
```

### Method 2: Authorization Bearer Header

```bash theme={null}
curl \
  --header "Authorization: Bearer c7142d5a-aba1-78ba-f521-189971e29c24" \
  http://127.0.0.1:8500/v1/kv/apps/eCommerce/database_host | jq
```

The JSON payload returned is identical to **Method 1**.

***

You’ve now learned how to authenticate Consul API requests using an ACL token—either via `X-Consul-Token` or the standard `Authorization: Bearer` header. For more information, refer to the [Consul API KV documentation](https://www.consul.io/api-docs/kv).

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/hashicorp-certified-consul-associate-certification/module/77c34744-e0fe-450e-82ea-c699ae223d45/lesson/3d7ed3e1-99f6-4e58-8317-2451357e5bbe" />

  <Card title="Practice Lab" icon="installation" cta="Learn more" href="https://learn.kodekloud.com/user/courses/hashicorp-certified-consul-associate-certification/module/77c34744-e0fe-450e-82ea-c699ae223d45/lesson/dda060e9-3724-4bdf-8bb6-bc592d749b37" />
</CardGroup>
