Call convex from a Node.js backend using service authentication
Hi, I need to call Convex from a Node.js backend using service authentication (per https://docs.convex.dev/auth#service-authentication).
What's the recommended way to use the admin key in the Node.js client? Do I have to pass it as an argument to every query/mutation, or can I set it once when initializing the client to avoid boilerplate?
I also noticed the internal setAdminAuth function. Is that intended for this use case?
I've identified a few potential approaches:
Using convex-better-auth: Generate an admin API key, use it to fetch a session token, and set that token in the Node.js client. (since im already using https://github.com/get-convex/better-auth)
Using Custom JWTs: Configure custom JWT authentication as per the docs, generate a long-lived token for my service, and pass it using setAuth().
Static Key (Less Ideal): Pass the CONVEX_API_KEY as an argument to every individual query/mutation, which seems verbose.
Authentication | Convex Developer Hub
Add authentication to your Convex app.
6 Replies
Thanks for posting in <#1088161997662724167>.
Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets.
- Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.)
- Use search.convex.dev to search Docs, Stack, and Discord all at once.
- Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI.
- Avoid tagging staff unless specifically instructed.
Thank you!
Service authentication doc is just saying to set up a shared secret, which would be passed in the server-to-server call and compared in your Convex function. There aren't any service authentication api's to support this, just a pattern.
Ok
I guess using custom jwt we can avoid passing the shared secret everytime
Custom JWT Provider | Convex Developer Hub
Configure Convex to work with custom JWT providers that don't implement full OIDC protocol, including setup and client-side integration.
Will generate jwt and pass it as auth header and make use of ctx.auth.getUserIdentity();
external service logic
convex/auth.config.ts
script to generate static keys that needs to be set as envs
If your server to server stuff is always on behalf of an authenticated user, regular auth works for sure