Call auth.api endpoints without user context #3019

Open
opened 2026-03-13 10:34:48 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @AEZ314 on GitHub (Mar 8, 2026).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

Making a call such as auth.api.listOrganizations strictly requires request headers to be passed, and calling it with a body parameter where I pass the used id won't work.

Current vs. Expected behavior

auth.api endpoints strictly require headers to be passed, whereas there are many cases the endpoints are needed to be called from the server without user context. I could bypass better-auth and directly use my ORM, but I think that's a very ugly solution. Is there something I'm missing out, is this a feature that will be implemented later?

What version of Better Auth are you using?

1.4.19

System info

{
  "system": {
    "platform": "darwin",
    "arch": "arm64",
    "version": "Darwin Kernel Version 24.6.0: Wed Oct 15 21:12:08 PDT 2025; root:xnu-11417.140.69.703.14~1/RELEASE_ARM64_T6020",
    "release": "24.6.0",
    "cpuCount": 12,
    "cpuModel": "Apple M2 Pro",
    "totalMemory": "32.00 GB",
    "freeMemory": "0.15 GB"
  },
  "node": {
    "version": "v24.4.0",
    "env": "development"
  },
  "packageManager": {
    "name": "npm",
    "version": "11.4.2"
  },
  "frameworks": [
    {
      "name": "svelte",
      "version": "^5.48.2"
    },
    {
      "name": "@sveltejs/kit",
      "version": "^2.50.1"
    }
  ],
  "databases": [
    {
      "name": "pg",
      "version": "^8.18.0"
    },
    {
      "name": "postgres",
      "version": "^3.4.8"
    },
    {
      "name": "drizzle",
      "version": "^0.45.1"
    }
  ],
  "betterAuth": {
    "version": "Unknown",
    "config": null,
    "error": "Cannot find module '$schema'\nRequire stack:\n- /Users/jack/Desktop/HelloWorld/ZaloStack/app/src/lib/server/auth.js"
  }
}

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

Backend

Auth config (if applicable)


Additional context

No response

Originally created by @AEZ314 on GitHub (Mar 8, 2026). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce Making a call such as auth.api.listOrganizations strictly requires request headers to be passed, and calling it with a body parameter where I pass the used id won't work. ### Current vs. Expected behavior auth.api endpoints strictly require headers to be passed, whereas there are many cases the endpoints are needed to be called from the server without user context. I could bypass better-auth and directly use my ORM, but I think that's a very ugly solution. Is there something I'm missing out, is this a feature that will be implemented later? ### What version of Better Auth are you using? 1.4.19 ### System info ```bash { "system": { "platform": "darwin", "arch": "arm64", "version": "Darwin Kernel Version 24.6.0: Wed Oct 15 21:12:08 PDT 2025; root:xnu-11417.140.69.703.14~1/RELEASE_ARM64_T6020", "release": "24.6.0", "cpuCount": 12, "cpuModel": "Apple M2 Pro", "totalMemory": "32.00 GB", "freeMemory": "0.15 GB" }, "node": { "version": "v24.4.0", "env": "development" }, "packageManager": { "name": "npm", "version": "11.4.2" }, "frameworks": [ { "name": "svelte", "version": "^5.48.2" }, { "name": "@sveltejs/kit", "version": "^2.50.1" } ], "databases": [ { "name": "pg", "version": "^8.18.0" }, { "name": "postgres", "version": "^3.4.8" }, { "name": "drizzle", "version": "^0.45.1" } ], "betterAuth": { "version": "Unknown", "config": null, "error": "Cannot find module '$schema'\nRequire stack:\n- /Users/jack/Desktop/HelloWorld/ZaloStack/app/src/lib/server/auth.js" } } ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript ``` ### Additional context _No response_
GiteaMirror added the enhancement label 2026-03-13 10:34:48 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Mar 8, 2026):

You have a few options for calling Better Auth API endpoints from the server without user context:

1. Use the userId parameter (where supported)
Some endpoints like createOrganization support a userId body parameter for server-side operations:

await auth.api.createOrganization({
  body: {
    name: "My Organization",
    slug: "my-org",
    userId: "some_user_id"  // Server-only parameter
  }
})

However, listOrganizations specifically requires a session by default and doesn't support this pattern.

2. API Key Plugin (for server-to-server calls)
Enable the API key plugin to use API keys as service account credentials. You'd still pass headers, but use an API key (x-api-key) instead of a user session.

3. Direct database access
For internal server operations, you can bypass the API layer entirely and query the database using Better Auth's internal adapter:

const context = await auth.$context;
const adapter = context.adapter;
// Use adapter methods to query data directly

The session requirement for admin APIs is an intentional security design, so there's no universal way to skip headers across all endpoints—but these approaches should cover your use case.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other

Chat with Dosu Open in Cursor Join Discord Share on X

@dosubot[bot] commented on GitHub (Mar 8, 2026): <!-- Answer --> You have a few options for calling Better Auth API endpoints from the server without user context: **1. Use the `userId` parameter (where supported)** Some endpoints like `createOrganization` support a [`userId` body parameter for server-side operations](https://github.com/better-auth/better-auth/blob/8e0e53ed9b8918f43dd8e60194580d1357f9e0fb/docs/content/docs/plugins/organization.mdx#L107-L115): ```typescript await auth.api.createOrganization({ body: { name: "My Organization", slug: "my-org", userId: "some_user_id" // Server-only parameter } }) ``` However, [`listOrganizations` specifically requires a session by default](https://github.com/better-auth/better-auth/blob/8e0e53ed9b8918f43dd8e60194580d1357f9e0fb/packages/better-auth/src/plugins/organization/routes/crud-org.ts#L874-L909) and doesn't support this pattern. **2. API Key Plugin (for server-to-server calls)** [Enable the API key plugin to use API keys as service account credentials](https://github.com/better-auth/better-auth/issues/4257#issuecomment-3481814655). You'd still pass headers, but use an API key (`x-api-key`) instead of a user session. **3. Direct database access** For internal server operations, you can [bypass the API layer entirely and query the database using Better Auth's internal adapter](https://github.com/better-auth/better-auth/discussions/4116#discussioncomment-14173980): ```typescript const context = await auth.$context; const adapter = context.adapter; // Use adapter methods to query data directly ``` The [session requirement for admin APIs is an intentional security design](https://github.com/better-auth/better-auth/issues/3717#issuecomment-3140046912), so there's no universal way to skip headers across all endpoints—but these approaches should cover your use case. <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/1c1d096a-195e-4d9e-8bc5-3b0de596bbc5?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/1c1d096a-195e-4d9e-8bc5-3b0de596bbc5?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/1c1d096a-195e-4d9e-8bc5-3b0de596bbc5?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/1c1d096a-195e-4d9e-8bc5-3b0de596bbc5?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/1c1d096a-195e-4d9e-8bc5-3b0de596bbc5?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/1c1d096a-195e-4d9e-8bc5-3b0de596bbc5?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/1c1d096a-195e-4d9e-8bc5-3b0de596bbc5?feedback_type=other)</sup> [![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github)&nbsp;[![Open in Cursor](https://dosu.dev/dosu-in-cursor.svg)](https://cursor.com/link/prompt?text=You%20have%20a%20few%20options%20for%20calling%20Better%20Auth%20API%20endpoints%20from%20the%20server%20without%20user%20context%3A%0A%0A%2A%2A1.%20Use%20the%20%60userId%60%20parameter%20%28where%20supported%29%2A%2A%20%20%0ASome%20endpoints%20like%20%60createOrganization%60%20support%20a%20%5B%60userId%60%20body%20parameter%20for%20server-side%20operations%5D%28https%3A//github.com/better-auth/better-auth/blob/8e0e53ed9b8918f43dd8e60194580d1357f9e0fb/docs/content/docs/plugins/organization.mdx%23L107-L115%29%3A%0A%0A%60%60%60typescript%0Aawait%20auth.api.createOrganization%28%7B%0A%20%20body%3A%20%7B%0A%20%20%20%20name%3A%20%22My%20Organization%22%2C%0A%20%20%20%20slug%3A%20%22my-org%22%2C%0A%20%20%20%20userId%3A%20%22some_user_id%22%20%20//%20Server-only%20parameter%0A%20%20%7D%0A%7D%29%0A%60%60%60%0A%0AHowever%2C%20%5B%60listOrganizations%60%20specifically%20requires%20a%20session%20by%20default%5D%28https%3A//github.com/better-auth/better-auth/blob/8e0e53ed9b8918f43dd8e60194580d1357f9e0fb/packages/better-auth/src/plugins/organization/routes/crud-org.ts%23L874-L909%29%20and%20doesn%27t%20support%20this%20pattern.%0A%0A%2A%2A2.%20API%20Key%20Plugin%20%28for%20server-to-server%20calls%29%2A%2A%20%20%0A%5BEnable%20the%20API%20key%20plugin%20to%20use%20API%20keys%20as%20service%20account%20credentials%5D%28https%3A//github.com/better-auth/better-auth/issues/4257%23issuecomment-3481814655%29.%20You%27d%20still%20pass%20headers%2C%20but%20use%20an%20API%20key%20%28%60x-api-key%60%29%20instead%20of%20a%20user%20session.%0A%0A%2A%2A3.%20Direct%20database%20access%2A%2A%20%20%0AFor%20internal%20server%20operations%2C%20you%20can%20%5Bbypass%20the%20API%20layer%20entirely%20and%20query%20the%20database%20using%20Better%20Auth%27s%20internal%20adapter%5D%28https%3A//github.com/better-auth/better-auth/discussions/4116%23discussioncomment-14173980%29%3A%0A%0A%60%60%60typescript%0Aconst%20context%20%3D%20await%20auth.%24context%3B%0Aconst%20adapter%20%3D%20context.adapter%3B%0A//%20Use%20adapter%20methods%20to%20query%20data%20directly%0A%60%60%60%0A%0AThe%20%5Bsession%20requirement%20for%20admin%20APIs%20is%20an%20intentional%20security%20design%5D%28https%3A//github.com/better-auth/better-auth/issues/3717%23issuecomment-3140046912%29%2C%20so%20there%27s%20no%20universal%20way%20to%20skip%20headers%20across%20all%20endpoints%E2%80%94but%20these%20approaches%20should%20cover%20your%20use%20case.)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/8495)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#3019