#Can multiple functions exist in one Appwrite cloud function?

11 messages · Page 1 of 1 (latest)

drowsy gulch
#

Can I add more than one function in the src/main.js file of an Appwrite cloud function?

What if the different functions depend on two separate events, specifically: user.*.create and bucket.*.files.*.create, would it still work?

shy drift
drowsy gulch
#

Thanks, Steven. However, do you mean I handle different scenarios in a single function?

shy drift
drowsy gulch
#

Oh ok…thanks

drowsy gulch
#

@shy drift please, how do I trigger my function using the http trigger in my application? do i call an endpoint or something?

drowsy gulch
#

Thanks

drowsy gulch
#

and I guess, calling a function with the SDK means the event trigger is http, right?

#

can I do something like this in my function:

module.exports.checkPermission = async (userId, resource, action) => {
  const response = await fetch(
    "https://cloud.appwrite.io/v1/functions/{FUNCTION_ID}/executions",
    {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        "X-Appwrite-Project": "YOUR_PROJECT_ID",
        "X-Appwrite-Key": "YOUR_API_KEY", // Consider using environment variables
      },
      body: JSON.stringify({ userId, resource, action }),
    }
  );

  const data = await response.json();
  return data.allowed; // true or false
};
shy drift