
Thank you so much ! This information will serve me as sure soon, but unfortunately it does not answer my question ^^
The problem is that I don't execute anything on the client side, the function is automatically triggered with an event (like creating a user)

In this case, how can I retrieve the user information that was created?

Ahhhh okay! In that case, you can use Realtime.

Realtime ?

Alright one sec

You can read detailed information about Realtime here: https://appwrite.io/docs/realtime

But in short, it's a way for you to subscribe to changes being made to anything in your Appwrite project

There's a lot of channels that you can subscribe to, which can be found here: https://appwrite.io/docs/realtime#channels

But we agree that I don't need to run anything with any SDK?

Realtime works best with the SDK that appwrite supplies

Have a look at the Realtime docs. It should help you

For example, if your function is triggered by a new document creation, you can use the new document's ID (which is returned after the document is created) to subscribe to any changes being made to that document

I hope that makes sense

I think I understood something. I'll try, but I don't think it's exactly the same thing I want to do. I'll be back in about ten minutes to try something then I'll let you know ^^

const { Client, Storage, Query, Permission } = require("node-appwrite")
module.exports = async function (req, res) {
const client = new Client()
const storage = new Storage(client)
client.setEndpoint(req.variables.APPWRITE_FUNCTION_ENDPOINT)
.setProject(req.variables.APPWRITE_FUNCTION_PROJECT_ID)
.setKey(req.variables.APPWRITE_FUNCTION_API_KEY)
const user = req.variables.APPWRITE_FUNCTION_EVENT_DATA
const { userId } = JSON.parse(user)
const bucketId = `user-${userId}`
const list = await storage.listBuckets([Query.equal("$id", [bucketId])])
if (list.total) {
res.send(`Bucket ${bucketId} already exists`, 409)
return
}
const bucket = await storage.createBucket(bucketId, bucketId, [
Permission.read(Role.user(userId)),
Permission.write(Role.user(userId)),
Permission.delete(Role.user(userId))
], true)
res.json(bucket, 201)
}
This is the kind of functions that will be triggered by the creation event of a user. For the moment I have a connection refused error when running, but the idea is there ^^

Look at the event data variable. See https://appwrite.io/docs/functions#functionVariables

Thank you ^^ Indeed I was able to retrieve the data, however I don't understand why I can't create a bucket :/ Here is my error and my code:
const { Client, Storage, Role, Permission } = require("node-appwrite")
module.exports = async function (req, res) {
const client = new Client()
const storage = new Storage(client)
client.setEndpoint("https://back.transferra.cc/v1")
.setProject(req.variables.APPWRITE_FUNCTION_PROJECT_ID)
.setKey(req.variables.APPWRITE_FUNCTION_API_KEY)
const user = req.variables.APPWRITE_FUNCTION_EVENT_DATA
const { userId } = JSON.parse(user)
const bucketId = `user-${userId}`
const bucket = await storage.createBucket(bucketId, bucketId, [
Permission.read(Role.user(userId)),
Permission.write(Role.user(userId)),
Permission.delete(Role.user(userId))
], true)
res.json(bucket, 201)
}
Error: User (role: guests) missing scope (buckets.write)
at Client.call (/usr/code-start/node_modules/node-appwrite/lib/client.js:171:31)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async Storage.createBucket (/usr/code-start/node_modules/node-appwrite/lib/services/storage.js:115:16)
at async module.exports (/usr/code-start/src/index.js:27:20)
at async /usr/local/src/server.js:68:13
I use an API key with rights to create buckets.

User guest means API key wasn't set 🧐 you created a variable and put in the API key? Maybe you can update your code to log the Appwrite function API key variable

Indeed error on my part, I had well defined the variable but the content of the key was wrong 🤣 I have two questions and then I think it's all good for me.
When I deploy a function via Appwrite CLI, the triggers get deleted every time and I have to recreate them from the appwrite interface. How to prevent this?
My second question: Is TypeScript supported?

You will need to put your variables and any other function settings in your appwrite.json.

For typescript, for now, you'll have to transpile before deploying and deploy the transpiled code.

My package.json has a build script: https://github.com/stnguyen90/places/blob/9c55f7097f79a12eeb7eda8fa450e4d961b07cc4/appwrite-functions/create-photo/package.json#L7
And then the entry point is the transpiled js file: https://github.com/stnguyen90/places/blob/9c55f7097f79a12eeb7eda8fa450e4d961b07cc4/appwrite.json.default#L438

Oh okay ! That's damn good lol A big thank you to you both steven and safwan ^^
The more I use Appwrite, the more I love it, the team too :'))

It's good ! Everything works great :)

[SOLVED] How functions work
Recommended threads
- Is Quick Start for function creation wor...
I am trying to create a Node.js function using the Quick Start feature. It fails and tells me that it could not locate the package.json file. Isn't Quick Start ...
- Connecting server functions to GitHub re...
The project I am working in has recently moved organizations on Appwrite. The same is true for the repo on GitHub, which as moved from a private user to a organ...
- Missing C++ libstdc library in Python fu...
I have a function running Python 3.12 which suddenly started dumping errors (as of today; it worked yesterday). I hadn't changed any code so I found this odd, b...
