Api Key Verification is not working #1157

Closed
opened 2026-03-13 08:25:31 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @devmdfaiz on GitHub (May 4, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

I am trying to verifying my api but is always return false and "API_KEY_INVALID". My code
const { valid, error, key } = await auth.api.verifyApiKey({ body: { key: apiKey, }, });

Current vs. Expected behavior

I have tried everything but this is not working

What version of Better Auth are you using?

1.2.7

Provide environment information

Windows 11

Which area(s) are affected? (Select all that apply)

Backend

Auth config (if applicable)

import { betterAuth } from "better-auth"
export const auth = betterAuth({
  emailAndPassword: {  
    enabled: true
  },
});

Additional context

No response

Originally created by @devmdfaiz on GitHub (May 4, 2025). ### Is this suited for github? - [ ] Yes, this is suited for github ### To Reproduce I am trying to verifying my api but is always return false and "API_KEY_INVALID". My code `const { valid, error, key } = await auth.api.verifyApiKey({ body: { key: apiKey, }, });` ### Current vs. Expected behavior I have tried everything but this is not working ### What version of Better Auth are you using? 1.2.7 ### Provide environment information ```bash Windows 11 ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ emailAndPassword: { enabled: true }, }); ``` ### Additional context _No response_
Author
Owner

@simon-dk commented on GitHub (May 5, 2025):

The "key" value stored in the database is hashed, so the value you need to run verify against, is the one returned directly when using the "await auth.api.createApiKey(..)" method. Its only visible this one time.

I.e.:

const { key } = await auth.api.createApiKey({
    headers: await headers(),
    body: {
      userId: '<user-id>',
      // ... other properties
    },
  });

const verified = await auth.api.verifyApiKey({
    body: {
      key,
    },
  });

console.log(verified)

Edit: Just looked at your config. You also need to add the apiKey() plugin in the config.

import { betterAuth } from 'better-auth';
import { apiKey } from 'better-auth/plugins';
import { Pool } from 'pg';

export const auth = betterAuth({
  database: new Pool({
    connectionString: process.env.BETTER_AUTH_DATABASE_URL,
  }),
  plugins: [apiKey()],
});
``
@simon-dk commented on GitHub (May 5, 2025): The "key" value stored in the database is hashed, so the value you need to run verify against, is the one returned directly when using the "await auth.api.createApiKey(..)" method. Its only visible this one time. I.e.: ```ts const { key } = await auth.api.createApiKey({ headers: await headers(), body: { userId: '<user-id>', // ... other properties }, }); const verified = await auth.api.verifyApiKey({ body: { key, }, }); console.log(verified) ``` Edit: Just looked at your config. You also need to add the apiKey() plugin in the config. ```ts import { betterAuth } from 'better-auth'; import { apiKey } from 'better-auth/plugins'; import { Pool } from 'pg'; export const auth = betterAuth({ database: new Pool({ connectionString: process.env.BETTER_AUTH_DATABASE_URL, }), plugins: [apiKey()], }); ``
Author
Owner

@devmdfaiz commented on GitHub (May 9, 2025):

It's now working in case (i am using hono)
Create logic-
await auth.api.createApiKey({ headers: c.req.raw.headers, body: { name: body.name, prefix: "smm_guru_live_", userId: user.id, }, });

Verification Logic-
const keyInfo = await auth.api.verifyApiKey({ body: { key: apiKey, }, });

My log-
Key info: { valid: false, error: { message: 'Invalid API key.', code: 'KEY_NOT_FOUND' }, key: null }

@devmdfaiz commented on GitHub (May 9, 2025): It's now working in case (i am using hono) Create logic- ` await auth.api.createApiKey({ headers: c.req.raw.headers, body: { name: body.name, prefix: "smm_guru_live_", userId: user.id, }, });` Verification Logic- ` const keyInfo = await auth.api.verifyApiKey({ body: { key: apiKey, }, });` My log- `Key info: { valid: false, error: { message: 'Invalid API key.', code: 'KEY_NOT_FOUND' }, key: null }`
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1157