Photo by Possessed Photography on Unsplash
My First Appwrite Function
Yesterday I shared how to run your Appwrite function on PHP runtime. Today, I will share the first Appwrite function I wrote in PHP. The function I will build is to save the QR code image returned by Appwrite Avatar API and upload it into Appwrite Storage.
Let's understand how the folder structure work
As you can see, once you create and deploy the function through the Appwrite CLI, there will be a folder structure like the above.
- This is the parent folder
- Folder with the appwrite.json config, which includes detail like project name, project id and details of each function. You need to trigger the deploy functions feature here within the terminal.
- Function folders listing- each folder represents a standalone Appwrite function
- Actual function folder - you can try to trigger the function locally when developing and debugging.
How to develop and test locally
Navigate into the Actual function folder and install the composer package; you can add any required package here; by default, the Appwrite SDK package has included.
Once the installation is complete, you can edit the src/index.php file; this will be the main file to run your process. You can see this portion within the default generate the file; the function will only execute when it deploys to the Appwrite instance; ignore it for now.
Let's add a simple line of code before the "return function above" to test that everything is working fine.
Make sure you are in the Actual function folder and run the script.
You should be able to see "Appwrite is awesome" on your terminal. You can continue to write your script here and test it locally until perfect, then move into the return function for testing on your Appwrite instance.
If you see the default index.php file that generates by Appwrite, you can see there are two crucial parameters, the $req and $res. The $req is for variables, and the $res is for functions to return. The explanation within the file is pretty straightforward, and I don't think it needs much more explanation.
Let's drill in to see more detail on the $req
- We can access most of these variables through $req['variables']['APPWRITE_XXXX'] within our index.php
- APPWRITE_FUNCTION_DATA is the input you key in when you execute the function manually
- APPWRITE_EVENT and APPWRITE_EVENT_DATA will be filled when your function triggers by events.
- You can add more variables by editing the function variables; it will be a good place to keep sensitive info, including the API key or token.
Function that I am going to build
I want to build a function that triggers when a new user is created. The function will capture the user id and convert it into a QR code image, and the Avatar API return the image content, not the actual file, which is why I need to convert it into an image and upload it through the Storage API.
Let's go through what does each function do?
- Initial the Appwrite client
- Grab the user id through the events that trigger it ( the user.create event )
- Use the Appwrite Avatar API to generate the user id to the QR code and save it as an image.
- Upload the image through the Storage API.
Once the function is ready, deploy it to the Appwrite instance. Remember to re-select event every time you re-deploy if you want your function to trigger by the events.
Don't forget to ensure the function variables are set to use in your function.
You can try to create a new user, and the function should create a QR code image in the storage!