Docs

Functions

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

You can deploy your code using the Appwrite CLI tool, the Appwrite Server API or manually from the Appwrite console.

Create Tag

  • Unix

    appwrite functions createTag \
        --functionId=6012cc93d5a7b \
        --command="node index.js" \
        --code="/myrepo/myfunction"
  • CMD

    appwrite functions createTag ^
        --functionId=6012cc93d5a7b ^
        --command="node index.js" ^
        --code="/myrepo/myfunction"
  • PowerShell

    appwrite functions createTag ,
        --functionId=6012cc93d5a7b ,
        --command="node index.js" ,
        --code="/myrepo/myfunction"

The command above accepts two parameters:

Name Description
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).

You can also deploy new code tags using the Appwrite server API

Handling Dependencies

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:

Python

PIP_TARGET=./.appwrite pip install -r ./requirements.txt --upgrade --ignore-installed

Dart

PUB_CACHE=./.appwrite pub get

Deno

DENO_DIR=./.appwrite deno cache main.ts

Ruby

docker run --rm -v $(pwd):/app -w /app --env GEM_HOME=./.appwrite appwrite/env-ruby-2.7:1.0.2 bundle install

Execute

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.

Function settings page. Function settings page.
Function settings page.

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.

  • Web

    let sdk = new Appwrite();
    
    sdk
        .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint
        .setProject('5df5acd0d48c2') // Your project ID
    ;
    
    let promise = sdk.functions.createExecution('[FUNCTION_ID]');
    
    promise.then(function (response) {
        console.log(response); // Success
    }, function (error) {
        console.log(error); // Failure
    });
  • Flutter

    import 'package:appwrite/appwrite.dart';
    
    void main() { // Init SDK
      Client client = Client();
      Functions functions = Functions(client);
    
      client
        .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint
        .setProject('5df5acd0d48c2') // Your project ID
      ;
    
      Future result = functions.createExecution(
        functionId: '[FUNCTION_ID]',
      );
    
      result
        .then((response) {
          print(response);
        }).catchError((error) {
          print(error.response);
      });
    }

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.

Supported Environments

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.

Name Image Architectures
Function Env. node-14.5 appwrite/env-node-14.5:1.0.0 x86 / ppc / arm
Function Env. node-15.5 appwrite/env-node-15.5:1.0.0 x86 / ppc / arm
Function Env. php-7.4 appwrite/env-php-7.4:1.0.0 x86 / ppc / arm
Function Env. php-8.0 appwrite/env-php-8.0:1.0.0 x86 / ppc / arm
Function Env. ruby-2.7 appwrite/env-ruby-2.7:1.0.2 x86 / ppc / arm
Function Env. ruby-3.0 appwrite/env-ruby-3.0:1.0.0 x86 / ppc / arm
Function Env. python-3.8 appwrite/env-python-3.8:1.0.0 x86 / ppc / arm
Function Env. deno-1.2 appwrite/env-deno-1.2:1.0.0 x86 / ppc / arm
Function Env. deno-1.5 appwrite/env-deno-1.5:1.0.0 x86 / ppc / arm
Function Env. deno-1.6 appwrite/env-deno-1.6:1.0.0 x86 / ppc / arm
Function Env. dart-2.10 appwrite/env-dart-2.10:1.0.0 x86
Function Env. dotnet-3.1 appwrite/env-dotnet-3.1:1.0.0 x86 / arm
Function Env. dotnet-5.0 appwrite/env-dotnet-5.0:1.0.0 x86 / arm

Environment Variables

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.

Name Description
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_DATA 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.
APPWRITE_FUNCTION_DATA

version >= 0.8.0

Your function's custom execution data. This variable's value contains a string in any format. If the custom data is in JSON FORMAT, it must be parsed inside the function code.
APPWRITE_FUNCTION_PROJECT_ID

version >= 0.8.0

Your function's project ID.
APPWRITE_FUNCTION_USER_ID

version >= 0.8.0

The userId of the user that triggered your function's execution. Executions triggered in the Appwrite console will be prepended with "admin-".
APPWRITE_FUNCTION_JWT

version >= 0.8.0

A JSON Web Token generated for the user that executes your function.
APPWRITE_FUNCTION_EVENT_PAYLOAD

version < 0.8.0 (deprecated)

Your function event payload. Deprecated in favor of APPWRITE_FUNCTION_EVENT_DATA in version 0.8.0.

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.

Function usage and logs tracking. Function usage and logs tracking.
Function usage and logs tracking.

Run Offline

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.

  • Unix

    docker run --rm --volume $(pwd):/usr/local/src:rw \
        --env MY_VALUE1="Hello World 1" \
        --env MY_VALUE2="Hello World 2" \
        appwrite/env-node-15.5:1.0.0 \
        node index.js
  • CMD

    docker run --rm --volume "%cd%":/usr/local/src:rw ^
        --env MY_VALUE1="Hello World 1" ^
        --env MY_VALUE2="Hello World 2" ^
        appwrite/env-node-15.5:1.0.0 ^
        node index.js"
  • PowerShell

    docker run --rm --volume ${pwd}:/usr/local/src:rw ,
        --env MY_VALUE1="Hello World 1" ,
        --env MY_VALUE2="Hello World 2" ,
        appwrite/env-node-15.5:1.0.0 ,
        node index.js"

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.