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

CleanShot 2022-10-30 at 20.09.28@2x.png

As you can see, once you create and deploy the function through the Appwrite CLI, there will be a folder structure like the above.

  1. This is the parent folder
  2. 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.
  3. Function folders listing- each folder represents a standalone Appwrite function
  4. 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.

composer install

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.

CleanShot 2022-10-30 at 20.21.48@2x.png

Make sure you are in the Actual function folder and run the script.

php src/index.php

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.

Function Parameters

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.

CleanShot 2022-10-30 at 20.27.34@2x.png

Let's drill in to see more detail on the $req

CleanShot 2022-10-30 at 20.29.48@2x.png

  • 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.

CleanShot 2022-10-30 at 20.40.53@2x.png

Let's go through what does each function do?

  1. Initial the Appwrite client
  2. Grab the user id through the events that trigger it ( the user.create event )
  3. Use the Appwrite Avatar API to generate the user id to the QR code and save it as an image.
  4. 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.

CleanShot 2022-10-30 at 20.44.41@2x.png

Don't forget to ensure the function variables are set to use in your function.

CleanShot 2022-10-30 at 20.45.53@2x.png

You can try to create a new user, and the function should create a QR code image in the storage!