nextauthjs/next-auth · error

Dgraph client error: Please provide an API key

Error message

Dgraph client error: Please provide an API key

What it means

The Dgraph adapter's client() factory validates its params before building the GraphQL client and throws if authToken is missing. Dgraph Cloud requires an API key for authorization, so the adapter refuses to construct a client without one. This is a configuration-time error thrown synchronously when the adapter is created.

Source

Thrown at packages/adapter-dgraph/src/lib/client.ts:37

  /**
   * @default "Authorization"
   *
   * [Using JWT and authorization claims](https://dgraph.io/docs/graphql/authorization/authorization-overview#using-jwts-and-authorization-claims)
   */
  authHeader?: string
}

export class DgraphClientError extends Error {
  name = "DgraphClientError"
  constructor(errors: any[], query: string, variables: any) {
    super(errors.map((error) => error.message).join("\n"))
    console.error({ query, variables })
  }
}

export function client(params: DgraphClientParams) {
  if (!params.authToken) {
    throw new Error("Dgraph client error: Please provide an API key")
  }
  if (!params.endpoint) {
    throw new Error(
      "Dgraph client error: Please provide a valid GraphQL endpoint"
    )
  }

  const {
    endpoint,
    authToken,
    jwtSecret,
    jwtAlgorithm = "HS256",
    authHeader = "Authorization",
  } = params
  const headers: HeadersInit = {
    "Content-Type": "application/json",
    "X-Auth-Token": authToken,
  }

View on GitHub (pinned to a1a16a5a77)

Solutions

  1. Set the authToken param: DgraphAdapter({ endpoint: process.env.DGRAPH_ENDPOINT, authToken: process.env.DGRAPH_AUTH_TOKEN }).
  2. Configure the env var/secret in your hosting platform (wrangler secret put, .env.local, etc.).
  3. Check for typos or falsy empty-string values in the token variable.

Example fix

// before
const adapter = DgraphAdapter({ endpoint: process.env.DGRAPH_ENDPOINT })
// after
const adapter = DgraphAdapter({
  endpoint: process.env.DGRAPH_ENDPOINT,
  authToken: process.env.DGRAPH_AUTH_TOKEN,
})
Defensive patterns

Strategy: validation

Validate before calling

if (!process.env.DGRAPH_AUTH_TOKEN) {
  throw new Error('DGRAPH_AUTH_TOKEN must be set before initializing the Dgraph adapter')
}

Type guard

function hasDgraphParams(p: unknown): p is Required<DgraphClientParams> {
  return !!p && typeof p === 'object' && typeof (p as any).authToken === 'string' && (p as any).authToken.length > 0
}

Try / catch

try {
  const adapter = DgraphAdapter(params)
} catch (e) {
  if (e instanceof Error && e.message.includes('Please provide an API key')) {
    // fail fast at startup: check env var name and deployment secrets
  }
  throw e
}

Prevention

When it happens

Trigger: Calling DgraphAdapter({ endpoint }) (or DgraphClient) without params.authToken, or with authToken set to an empty string/undefined from a missing env var.

Common situations: DGRAPH_AUTH_TOKEN (or equivalent) env var not set in the deployment (Cloudflare Workers secrets, Vercel env), typo in the env var name, or hardcoding params without the token in local dev.

Understand the failure class

Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.

Related errors


AI-assisted analysis of nextauthjs/next-auth@a1a16a5a77 (2026-08-28). Data as JSON: /api/errors/f5f9017f4f5cf3b6. Report an issue: GitHub.