Back

[SOLVED] Invoking Appwrite Functions via REST API

  • 0
  • Functions
RAKU
18 Aug, 2023, 15:24

Hello everyone,

I recently started using the Appwrite cloud service and was experimenting with its functions feature.

Previously, I used AWS Lambda, and now I want to switch to Appwrite.

In AWS Lambda, you can trigger a function by sending an HTTP request to its function URL. In Appwrite, it seems we can achieve this by sending a POST request to the following URL: 'https://cloud.appwrite.io/v1/v1/functions/functionId/executions'.

However, I'm facing an issue when I try to invoke a function I created by sending a POST request using a tool like Postman. The error message I'm encountering is:

TypeError: Cannot set properties of undefined (setting 'functions') at module.exports (/usr/code-start/index.js:19:20) at /usr/local/src/server.js:68:19 at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

The function works perfectly fine when I manually trigger it from the Appwrite console.

Also, I'm sending the request with all the necessary headers, including X-Appwrite-Project, Content-Type, and X-Appwrite-Key, along with a correct JSON body. Here is the demo function code I am using:

module.exports = async (req, res) => {

TypeScript
let options = JSON.parse(req.payload);
res.json(options);
  

};

Could someone please assist me in resolving this issue? Your help would be greatly appreciated.

Thank you.

TL;DR
User wants to know the maximum memory for Appwrite cloud functions and if it can be configured. Another user answers that the default memory is 512mb and it cannot be configured at the moment. User had a problem invoking Appwrite functions via REST API. They were able to solve it by passing the stringified JSON in a data key in the request body. User encountered an issue with the payload being empty when invoking a function. They have verified the function code and the headers. They are seeking assistance in resolving the issue.
Drake
18 Aug, 2023, 15:48

FYI, it's best to wrap code in backticks to format a bit nicer. You can use 1 backtick for inline code (https://www.markdownguide.org/basic-syntax/#code) and 3 backticks for multiline code (https://www.markdownguide.org/extended-syntax/#syntax-highlighting.

What are you passing for hte JSON body?

Drake
18 Aug, 2023, 15:49

also that url has 2 v1 in the path and you changed the function id for your actual function id, right?

Drake
18 Aug, 2023, 15:49

where do you see this error? in the Appwrite Console function logs (errors tab)?

RAKU
18 Aug, 2023, 16:25

Hey Stevan,

Thanks for your response.

I am using the default JSON for the test

TypeScript
{
    "firstName": "hello", 
    "lastName":"world", 
    "age":"old"
}

'also that url has 2 v1 in the path and you changed the function id for your actual function id, right? '

Yes I changed the function ID, and only v1 1 time only (added 2 v1 by mistake here)

I tried to create a new function again with the following code:

TypeScript
module.exports = async (req, res) => {
    res.json({reqData: req,
        payload: req.payload});
      
};

Now I am not getting the error I was getting previously, however, I noticed that the payload is always empty even though I provide a valid json body in the postman.

Drake
18 Aug, 2023, 16:40

would you please stringify your JSON and then put this as your request body:

TypeScript
{
  "data": "STRINGIFIED JSON"
}

see https://appwrite.io/docs/client/functions?sdk=rest-default#functionsCreateExecution

RAKU
18 Aug, 2023, 16:58

Ohh,

It Worked. Didn't know that I have to pass the stringified JSON in a data key.

Thank you so much 🙂

Drake
18 Aug, 2023, 17:01

[SOLVED] Invoking Appwrite Functions via REST API

RAKU
18 Aug, 2023, 17:45

Hey Steven,

BTW may I know what is the max memory of appwrite cloud functions?

In AWS Lambda we can change the memory for each function from 128 mb to 3008mb.

So whats the default memory set on appwrite for functions? Or can we change it?

Drake
18 Aug, 2023, 18:57

512mb. you can't configure it at the moment

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