Back

[SOLVED] Inside function: The current user is not authorized to perform the requested action

  • 0
  • Databases
  • Functions
  • Self Hosted
wintercounter✌🏼
10 Oct, 2023, 06:17

I'm getting the above error when I'm trying to call listDocuments inside a function. Aren't default clients using APPRITE_API_KEY in functions supposed to be able to make actions regardless of permissions? Or I'm doing something wrong?

Here is the function:

TypeScript
import { Client, Query, Databases } from 'node-appwrite'

const client = new Client()
client
    .setEndpoint(process.env.APPWRITE_ENDPOINT)
    .setProject(process.env.APPWRITE_FUNCTION_PROJECT_ID)
    .setKey(process.env.APPWRITE_API_KEY)

const db = new Databases(client)

export default async ({ req, res, log, error }) => {
    const results = await db.listDocuments('MYDB', 'Users', [Query.equal('UserID', req.body.userId)])
    log(results)
    return res.send('ok')
}
TL;DR
The user is encountering an error message "The current user is not authorized to perform the requested action" when calling the `listDocuments` function inside their code. They are using the `node-appwrite` package and the error seems to be related to permissions. They are using the default API key provided by Appwrite, but it is not working. The user is unsure if they need to set the API key themselves or if there is an issue with their code. Solution: The user should check if the API key they are using has the necessary permissions to access the database scopes. They can also try setting the API key themselves and see
Maniac_Fighter
10 Oct, 2023, 06:50

does your api key have access to database scopes?

wintercounter✌🏼
10 Oct, 2023, 06:57

wdym? the api key is coming directly from appwrite, it's not set by me

wintercounter✌🏼
10 Oct, 2023, 07:02

I guess this is the key named Generated for Template

Maniac_Fighter
10 Oct, 2023, 07:25

could you share the execution logs of the function?

wintercounter✌🏼
10 Oct, 2023, 07:27

i just logged out process.env.APPWRITE_API_KEY and it's undefined

wintercounter✌🏼
10 Oct, 2023, 07:28

i'm invoking this function with an event, can that cause such problem?

wintercounter✌🏼
10 Oct, 2023, 07:31

hmm, seems like I need to set this on my own, the docs isn't really clear about this

wintercounter✌🏼
10 Oct, 2023, 07:32

I expected to have the default generated always available

Maniac_Fighter
10 Oct, 2023, 07:32

which template you are using?

Maniac_Fighter
10 Oct, 2023, 07:32

nope. events are only used to trigger the function when certain events happen

wintercounter✌🏼
10 Oct, 2023, 07:35

Honestly, I have no clue 😄 For sure I didn't generate this API key on my own, it's just there in my project 😄

Maniac_Fighter
10 Oct, 2023, 07:36

happens😆

Maniac_Fighter
10 Oct, 2023, 07:36

Just executing any function, make sure that the enviroment vars are set

Maniac_Fighter
10 Oct, 2023, 07:36

this could be the reason

wintercounter✌🏼
10 Oct, 2023, 07:36

yeah, it was

wintercounter✌🏼
10 Oct, 2023, 07:37

now it's all good

Maniac_Fighter
10 Oct, 2023, 07:41

Awesome!

Drake
11 Oct, 2023, 02:11

[SOLVED] Inside function: The current user is not authorized to perform the requested action

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