Appwrite Functions allow you to extend and customize your Appwrite server functionality by executing your custom code. Appwrite can execute your custom code in response to any Appwrite system event like account creation, user login, or document update. You can also schedule your functions to run according to a CRON schedule or start them manually by triggering your function from an HTTP endpoint using the Appwrite client or server APIs.
Appwrite Functions run in a secure, isolated Docker container. By default, Appwrite supports multiple environments for different languages that you can use to run your code.
Add Your Function
You can add a new function from your Appwrite project's dashboard. Access your Functions settings from your projects left navigation panel. Click the 'Add Function' button and choose your function name and code environment. In your Functions settings page, you can set your function event triggers, CRON schedule and set secure environment variables for your function environment.
Deploy Your Code
The command above accepts two parameters:
|functionId||The ID of the Function you created in the previous step. You can find your function ID on your function page in your project dashboard.|
|command||The default command to execute when your function is triggered.|
|code||Gzip file with your code package. When used with the Appwrite CLI, pass the path to your code directory, and the CLI will automatically package your code. |
For increased security, The Appwrite CLI only has access to file paths within the current working directory. In other words, it doesn't support file paths that try to access the parent directories (../../mycode).
If your code uses a package manager like NPM, Yarn, or Composer to install packages, you have to make sure your code directory includes your packages directory (node_modules for node or vendor for PHP) before you try to package and deploy your code.
If you're using a programming language that saves your packages under a global directory like Dart, Python or Ruby, you should set your package manager to keep your code under the '.appwrite' code directory under your code working directory to be deployed and available in your cloud functions environment.
For most languages, this can be achieved easily by adding an environment variable before installing your dependencies. Below you can find some simple examples:
Besides setting a schedule or allowing your function to listen to Appwrite’s system events, you can also manually execute your cloud functions from your Appwrite console or API.
To execute a function from the Appwrite console, click the 'Execute Now' button on your function's overview page. To execute a function from the API, send a POST request to the function execution endpoint.
The function execution endpoint is available from both Appwrite client and server APIs. To execute your function from the server API, you need an API key with 'execution.write' scope.
Executing the function from the client API requires the current user to have execution permission for the function. You can change the execution permission from the function's settings page in the Appwrite console, by default no user, team, or role has this permission.
Abuse and Limits
Appwrite allows your project's end-users to execute Cloud Functions using client API or your client SDK. Execution is permitted to any user who has been granted the "execute" permission in your Cloud Functions settings page. The execution permission can accept any of the typical Appwrite permission types.
When triggering a Cloud Function execution from the client, your users will be limited to a specific amount of execution per minute to make sure your Appwrite server is not being abused. The default limit is 60 calls per 1 minute. You can edit this limit using the server environment variables.
Appwrite provides multiple code environments to execute your custom functions. Each environment uses a Docker image tied to a specific language version to provide a safe, isolated playground to run your team's code.
Below is a list of supported Cloud Functions environments. The Appwrite team continually adds support for new environments. You can easily change which environments your Appwrite setup supports by editing your server environment variables.
|Node.js 14.5||appwrite/env-node-14.5:1.0.0||x86 / ppc / arm|
|Node.js 15.5||appwrite/env-node-15.5:1.0.0||x86 / ppc / arm|
|PHP 7.4||appwrite/env-php-7.4:1.0.0||x86 / ppc / arm|
|PHP 8.0||appwrite/env-php-8.0:1.0.0||x86 / ppc / arm|
|Ruby 2.7||appwrite/env-ruby-2.7:1.0.2||x86 / ppc / arm|
|Ruby 3.0||appwrite/env-ruby-3.0:1.0.0||x86 / ppc / arm|
|Python 3.8||appwrite/env-python-3.8:1.0.0||x86 / ppc / arm|
|Deno 1.2||appwrite/env-deno-1.2:1.0.0||x86 / ppc / arm|
|Deno 1.5||appwrite/env-deno-1.5:1.0.0||x86 / ppc / arm|
|Deno 1.6||appwrite/env-deno-1.6:1.0.0||x86 / ppc / arm|
|.NET 3.1||appwrite/env-dotnet-3.1:1.0.0||x86 / arm|
|.NET 5.0||appwrite/env-dotnet-5.0:1.0.0||x86 / arm|
Environment variables supplied by Appwrite in addition to your own defined environment variables that you can access from your function code. These variables give you information about your execution environment.
|APPWRITE_FUNCTION_ID||Your function's unique ID.|
|APPWRITE_FUNCTION_NAME||Your function's name.|
|APPWRITE_FUNCTION_TAG||Your function's code tag unique ID.|
|APPWRITE_FUNCTION_TRIGGER||The name of the event that triggered that executed the function. This value can be any of Appwrite system events, 'api' if your function was triggered manually or by an API call, or 'schedule' if your CRON schedule triggered it.|
|APPWRITE_FUNCTION_ENV_NAME||Your function environment name. Can be any of Appwrite supported execution environments.|
|APPWRITE_FUNCTION_ENV_VERSION||Your function environment version.|
|APPWRITE_FUNCTION_EVENT||Your function event name. This value is available only when your function trigger is 'event.' This variable value can be any of Appwrite system events.|
|APPWRITE_FUNCTION_EVENT_PAYLOAD||Your function event payload. This value is available only when your function trigger is 'event'. This variable value contains a string in JSON format with your specific event data.|
Monitor & Debug
You can monitor your function execution usgae stats and logs from both your Appwrite console and the Appwrite server API. To access your functions usage stats and logs from your console, click the 'Usage' tab in your function dashboard.
The usage screen in your console will allow you to track the number of execution and your function CPU usage time. You can also review a detailed log of your function execution history, including the function exit code, output log, and error log.
You can quickly run your Cloud Function locally by executing the Appwrite environment locally using its corresponding Docker image. You can pass your offline container any environment variables your script needs and your entry command like in the example below.
The example above shows how you can mount your current working dir into the "appwrite/env-node-15.5:1.0.0" Cloud Functions environment and execute a custom command to simulate exactly how the Appwirte server will execute the function on the Appwrite server. You can also pass multiple environment variables that your code might require.
When running a Cloud Functions environment for a coding language that uses a central directory for managing all dependencies, you will need to package all your dependencies in your working directory under the ".appwrite" folder as mentioned in the "Handling Dependencies" step above. This will ensure your function container can access all required packages and execute your function successfully.
Demos & Examples
There are many Cloud Function demos and examples created by the Appwrite team and community in multiple coding languages. These examples are available at our demos repository on GitHub. You can also submit your examples by submitting a pull-request.