DEV Community

Cover image for Dynatrace API token: troubleshooting scopes and 401 and 403 errors (OTLP, API v2)
Span Observer
Span Observer

Posted on Originally published at span.observer

Dynatrace API token: troubleshooting scopes and 401 and 403 errors (OTLP, API v2)

Your OpenTelemetry Collector is running, yet nothing reaches Dynatrace. Its logs repeat a single line: 401 Unauthorized or 403 Forbidden. Nine times out of ten, the culprit is the Dynatrace API token: wrong header format, missing scope, expired token or a token sent to the wrong environment.

This kind of troubleshooting is expensive on call: while you search, traces, metrics and logs are lost, and the SLOs computed on them become wrong. A methodical diagnosis brings that time down to a few minutes and reduces the MTTR of ingestion incidents.

The goal of this tutorial: find out in under ten minutes why a request is rejected, for OTLP ingestion or for API v2. The content applies to Dynatrace SaaS and Dynatrace Managed; differences in URLs and tokens are flagged at each step.

401 or 403: what the code really says

The two codes do not tell the same story. Telling them apart stops you going round in circles.

Code Meaning Common causes
401 The caller is not authenticated Missing or malformed header, token revoked, expired, badly copied, or belonging to another environment
403 The caller is authenticated but not allowed Missing scope on the token, token disabled in some cases, restriction on the environment side
404 The URL matches nothing Wrong environment ID, /e/<env-id> path forgotten on Managed

Remember the rule: a 401 is fixed on the token or header, a 403 is fixed on the scopes. The error message returned in the response body often settles the question: always read it.

Prerequisites

  • A Dynatrace SaaS or Managed environment, and the right to manage tokens (appropriate environment permission).
  • curl and jq on a machine that can reach the environment (or the ActiveGate).
  • The token to test in an environment variable, never in plain text in a versioned file.
  • A second valid access token for the verification step: the lookup call accepts a classic token with any scope (or a platform token with api-tokens:tokens:read).

Troubleshooting a Dynatrace API token step by step

Step 1: check the environment URL

Before blaming the token, check the address. Formats differ by deployment:

# Dynatrace SaaS
export DT_URL="https://<env-id>.live.dynatrace.com"

# Dynatrace Managed (cluster)
export DT_URL="https://<your-domain>/e/<env-id>"

# Through an Environment ActiveGate (SaaS or Managed)
export DT_URL="https://<activegate>:9999/e/<env-id>"

export DT_API_TOKEN="dt0c01.XXXX.YYYY"
Enter fullscreen mode Exit fullscreen mode

A token created in environment A does not work in environment B. On multi-environment Managed, this is the first cause of 401 we come across.

Step 2: check the header format

The environment API expects the Api-Token scheme, not Bearer:

curl -s -o /dev/null -w "%{http_code}\n" \
  -H "Authorization: Api-Token ${DT_API_TOKEN}" \
  "${DT_URL}/api/v2/metrics?pageSize=1"
Enter fullscreen mode Exit fullscreen mode

Critical lines: the word Api-Token followed by a space, then the full token. A classic token starts with dt0c01. and contains two dot separators. If the string is truncated (copied from a narrow terminal, line break in a Kubernetes secret), you get a 401.

On SaaS, platform tokens and OAuth clients used by apps and the platform API are sent with Bearer. The OTLP API accepts both families, each with its own scheme and scopes (openpipeline:traces:ingest and so on for a platform token). Do not mix them: a platform token sent with Api-Token will be rejected. Check which kind of token each API expects on the authentication page of the documentation.

Step 3: read the token’s actual scopes

API v2 lets you look up a token’s metadata without showing it in the UI. The call uses the second token, and the token to inspect goes in the body:

curl -s -X POST "${DT_URL}/api/v2/apiTokens/lookup" \
  -H "Authorization: Api-Token ${DT_ADMIN_TOKEN}" \
  -H "Content-Type: application/json" \
  -d "{\"token\": \"${DT_API_TOKEN}\"}" | jq '{name, enabled, expirationDate, scopes}'
Enter fullscreen mode Exit fullscreen mode

The response shows whether the token is enabled, its expiry date and its list of scopes. Three common cases:

  • enabled: false: someone disabled it, often after a secret leak alert;
  • expirationDate in the past: the token has expired, create a new one;
  • expected scope missing: that explains your 403.

Step 4: match scope and call

Each endpoint requires a specific scope. Here are the most common mappings; check the exact name on the token creation screen, which shows both the label and the technical identifier:

Use Endpoint Scope
OTLP traces /api/v2/otlp/v1/traces openTelemetryTrace.ingest
OTLP metrics /api/v2/otlp/v1/metrics metrics.ingest
OTLP logs /api/v2/otlp/v1/logs logs.ingest
Read entities /api/v2/entities entities.read
Read problems /api/v2/problems problems.read
Settings objects /api/v2/settings/objects settings.read / settings.write

Classic trap: a Collector sending traces, metrics and logs with a single token that only has openTelemetryTrace.ingest. Traces go through, the other two pipelines return 403. The Collector then exports only part of the telemetry, which is harder to spot than a total outage.

Step 5: fix the Collector configuration

A clean exporter configuration, with the token injected through an environment variable:

exporters:
  otlphttp/dynatrace:
    endpoint: "${env:DT_URL}/api/v2/otlp"
    headers:
      Authorization: "Api-Token ${env:DT_API_TOKEN}"

service:
  pipelines:
    traces:
      receivers: [otlp]
      processors: [batch]
      exporters: [otlphttp/dynatrace]
    metrics:
      receivers: [otlp]
      processors: [cumulativetodelta, batch]
      exporters: [otlphttp/dynatrace]
    logs:
      receivers: [otlp]
      processors: [batch]
      exporters: [otlphttp/dynatrace]
Enter fullscreen mode Exit fullscreen mode

Critical lines: endpoint stops at /api/v2/otlp (the exporter appends /v1/traces, /v1/metrics and /v1/logs); the header uses the Api-Token scheme. For Collector details, see the OpenTelemetry Collector setup guide for Dynatrace.

On Kubernetes, create the secret without a stray line break:

kubectl create secret generic dynatrace-otlp \
  --from-literal=DT_API_TOKEN="${DT_API_TOKEN}" \
  -n observability
Enter fullscreen mode Exit fullscreen mode

Avoid echo "$TOKEN" | base64 in a manifest: echo adds a trailing newline, encoded with the rest, and the token received becomes invalid.

How to check that it works

  1. Replay the call from step 2: the expected code is 200.
  2. Send a minimal test trace to the OTLP endpoint; the Dynatrace OTLP API accepts binary protobuf only (no JSON); an empty protobuf body is enough to test authentication:

    curl -s -o /dev/null -w "%{http_code}\n" -X POST \
      -H "Authorization: Api-Token ${DT_API_TOKEN}" \
      -H "Content-Type: application/x-protobuf" \
      --data-binary "" \
      "${DT_URL}/api/v2/otlp/v1/traces"
    

    A 2xx code confirms authentication and scope; a 401 or 403 sends you back to steps 2 to 4.

  3. In the Collector logs, export errors should disappear: kubectl logs deploy/otel-collector -n observability | grep -iE "401|403" returns nothing.

  4. On SaaS, a DQL query confirms data arrival: fetch spans, from:now()-15m | summarize count(), by:{service.name}. On Managed, open the services or distributed traces view and look for the sending service.

If data is missing while authentication succeeds, the problem lies elsewhere: see OpenTelemetry traces not showing in Dynatrace.

Best practices

  • One token per use: an ingest token for the Collector, another for automation. A leak then exposes only a narrow scope.
  • Least privilege: add only the scopes you need. An ingest token has no reason to write settings.
  • An expiry date and planned rotation, with an alert before the deadline rather than an outage on the day.
  • An explicit name: team, use and date in the token name. The lookup becomes instantly readable.
  • Monitor ingestion: an alert on a drop in received volume catches an expired token before users do. See monitoring the OpenTelemetry Collector.
  • On Managed, keep in mind the split between environment tokens and Cluster API tokens, covered in automation with the Cluster API.

Frequently asked questions

Can I add a scope to an existing token?

Depending on the version, the UI lets you edit some tokens, but the secret value is never shown again. The safest practice is still to create a new token with the right scopes, deploy it, then revoke the old one.

Why do I get a 401 with a token that works in curl?

Compare the string the application actually receives: extra quotes in a YAML file, a trailing newline in a secret, an unresolved environment variable (the Collector then literally sends ${env:DT_API_TOKEN}). Temporarily enable client debug logs to check the header, and never keep them in production.

Does a Managed token work after migrating to SaaS?

No. Tokens are tied to their environment. After a migration, recreate the tokens in the SaaS environment and update every client that uses them.

Conclusion

A 401 is solved on the token or header, a 403 on the scopes, a 404 on the URL. With the test call and the token lookup, the diagnosis fits in a few commands. Add planned rotation and an alert on ingested volume: the next expiry will no longer be an incident.

span.observer is an independent community blog, not affiliated with Dynatrace LLC. Dynatrace is a trademark of its owner. Scope and screen names change: check them in the official documentation for your version.


Originally published on span.observer. French version: https://span.observer/fr/depanner-token-api-dynatrace/.

Top comments (0)