Skip to content
Back

Accessing caller UserID on Function Excecution

  • 0
  • Functions
Maastonakki
4 Jun, 2025, 16:54

I'm working with function, which should generate a document in Collection B, but only if user who's calling the function, is the owner of Document in Collection A.

Function is getting documentId as parameter through request body, but I'm wondering how can I access which user is making the actual function call, since I should fetch the documentId document from Collection A, and compare if caller matched documentA.ownerId field.

I'm excecuting the function through node-appwrite sessionClient, so call is made as logged in user, and actual call permission is checked by Functions own excecution access.

TL;DR
To access the user ID of the caller making a function call, use `const userId = req.headers['x-appwrite-user-id'];`. Compare this 'userId' with the 'documentA.ownerId' field to check if the caller is the owner of the document in Collection A. This will require fetching the document from Collection A. When using `node-appwrite` sessionClient for function execution, the call is made as the logged-in user and permissions are checked by the function's own execution access.
Lexy
4 Jun, 2025, 16:58

You can get the authenticated user id with const userId = req.headers['x-appwrite-user-id'];

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more