Develop Appwrite Functions

Appwrite Functions offer a familiar interface if you've developed REST endpoints. Each function is handled following a request and response pattern.

Lifecycle

There is a clear lifecycle for all Appwrite Functions, from beginning to end. Here's everything that happens during a function execution.

  1. The function is invoked.

  2. The active deployment's executor will handle the request.

  3. The Executor passes in request information like headers, body or path through the context.req object of your exported function.

  4. The runtime executes the code you defined, you can log through the context.log() or context.error() methods.

  5. Function terminates when you return results using return context.res.send(), return context.res.json() or similar.

Entrypoint

You'll find all of these steps in a simple function like this. Notice the exported entry point that the executor will call.

import { Client } from 'node-appwrite';

// This is your Appwrite function
// It's executed each time we get a request
export default async ({ req, res, log, error }) => {
  // Why not try the Appwrite SDK?
  //
  // const client = new Client()
  //    .setEndpoint('https://cloud.appwrite.io/v1')
  //    .setProject(process​.env.APPWRITE_FUNCTION_PROJECT_ID)
  //    .setKey(process​.env.APPWRITE_API_KEY);

  // You can log messages to the console
  log('Hello, Logs!');

  // If something goes wrong, log an error
  error('Hello, Errors!');

  // The `req` object contains the request data
  if (req.method === 'GET') {
    // Send a response with the res object helpers
    // `res.send()` dispatches a string back to the client
    return res.send('Hello, World!');
  }

  // `res.json()` is a handy helper for sending JSON
  return res.json({
    motto: 'Build like a team of hundreds_',
    learn: 'https://appwrite.io/docs',
    connect: 'https://appwrite.io/discord',
    getInspired: 'https://builtwith.appwrite.io',
  });
};

If you prefer to learn through more examples like this, explore the examples page.

Context object

Context is an object passed into every function to handle communication to both the end users, and logging to the Appwrite Console. All input, output, and logging must be handled through the context object passed in.

You'll find these properties in the context object.

PropertyDescription
reqContains request information like method, body, and headers. See full examples in the request section.
resContains methods to build a response and return information. See full examples in the response section.
log()Method to log information to the Appwrite Console, end users will not be able to see these logs. See full examples in the logging section.
error()Methoc to log errors to the Appwrite Console, end users will not be able to see these errors. See full examples in the logging section.

Destructuring assignment

Some languages, namely JavaScript, support destructuring. You'll see us use destructuring in examples, which has the following syntax.

Learn more about destructuring assignment.

// before destructuring
export default async function (context) {
    context.log("This is a log!");
    return context.res.send("This is a response!");
}

// after destructuring
export default async function ({ req, res, log, error }) {
    log("This is a log!");
    return res.send("This is a response!");
}

Request

If you pass data into an Appwrite Function, it'll be found in the request object. This includes all invocation inputs from Appwrite SDKs, HTTP calls, Appwrite events, or browsers visiting the configured domain. Explore the request object with the following function, which logs all request params to the Appwrite Console.

export default async ({ req, res, log }) => {
    log(req.bodyRaw);                     // Raw request body, contains request data
    log(JSON.stringify(req.body));        // Object from parsed JSON request body, otherwise string
    log(JSON.stringify(req.headers));     // String key-value pairs of all request headers, keys are lowercase
    log(req.scheme);                      // Value of the x-forwarded-proto header, usually http or https
    log(req.method);                      // Request method, such as GET, POST, PUT, DELETE, PATCH, etc.
    log(req.url);                         // Full URL, for example: http://awesome.appwrite.io:8000/v1/hooks?limit=12&offset=50
    log(req.host);                        // Hostname from the host header, such as awesome.appwrite.io
    log(req.port);                        // Port from the host header, for example 8000
    log(req.path);                        // Path part of URL, for example /v1/hooks
    log(req.queryString);                 // Raw query params string. For example "limit=12&offset=50"
    log(JSON.stringify(req.query));       // Parsed query params. For example, req.query.limit

    return res.send("All the request parameters are logged to the Appwrite Console.");
};

Headers

Appwrite Functions will always receive a set of headers that provide meta data about the function execution. These are provided alongside any custom headers sent to the function.

VariableDescription
x-appwrite-triggerDescribes how the function execution was invoked. Possible values are http, schedule or event.
x-appwrite-eventIf the function execution was triggered by an event, describes the triggering event.
x-appwrite-user-idIf the function execution was invoked by an authenticated user, display the user ID. This doesn't apply to Appwrite Console users or API keys.
x-appwrite-user-jwtJWT token generated from the invoking user's session. Used to authenticate Server SDKs to respect access permissions. Learn more about JWT tokens.
x-appwrite-country-codeDisplays the country code of the configured locale.
x-appwrite-continent-codeDisplays the continent code of the configured locale.
x-appwrite-continent-euDescribes if the configured local is within the EU.

Response

If you need to send a response to the invoker of the function, such as a user, client app, or an integration, use the response object. The response information will not be logged to the Appwrite Console. There are several possible ways to send a response, explore them in the following Appwrite Function.

export default async ({ req, res, log }) => {

    switch (req.query.type) {
        case 'empty': 
            return res.empty();
        case 'json':
            return res.json({"type": "This is a JSON response"});
        case 'redirect':
            return res.redirect("https://appwrite.io", 301);
        case 'html':
            return res.send(
                "<h1>This is an HTML response</h1>", 200, {
                    "content-type": "text/html"
                });
        default:
            return res.send("This is a text response");
    }
}

To get the different response types, set one of the following query parameters in the generated domain of your function.

TypeQuery ParamExample
text/?type=texthttps://64d4d22db370ae41a32e.appwrite.global/?type=text
json/?type=jsonhttps://64d4d22db370ae41a32e.appwrite.global/?type=json
redirect/?type=redirecthttps://64d4d22db370ae41a32e.appwrite.global/?type=redirect
html/?type=htmlhttps://64d4d22db370ae41a32e.appwrite.global/?type=html
empty/https://64d4d22db370ae41a32e.appwrite.global/

Logging

To protect user privacy, the request and response objects are not logged to the Appwrite Console by default. This means, to see logs or debug function executions you need to use the log() and error() methods. These logs are only visible to developers with access to the Appwrite Console.

Here's an example of using logs and errors.

export default async ({ req, res, log, error }) => {
    log("This is a log, use for logging information to console");
    log(`This function was called with ${req.method} method`);
    error("This is an error, use for logging errors to console");

    return res.send("Check the Appwrite Console to see logs and errors!");
};

You can access these logs through the following steps.

  1. In Appwrite Console, navigate to Functions.

  2. Click to open a function you wish to inspect.

  3. Under the Executions tab, click on an execution.

  4. In the Response section, you'll be able to view logs under the Logs and Errors tabs.

Accessing environment variables

If you need to pass constants or secrets to Appwrite Functions, you can use environment variables.

Appwrite API keys

If your function is using an Appwrite SDK with an API key, this API key needs to be generated and passed in manually. API keys are not passed by default for security reasons.

VariableDescription
APPWRITE_FUNCTION_IDThe ID of the running function.
APPWRITE_FUNCTION_NAMEThe Name of the running function.
APPWRITE_FUNCTION_DEPLOYMENTThe deployment ID of the running function.
APPWRITE_FUNCTION_PROJECT_IDThe project ID of the running function.
APPWRITE_FUNCTION_RUNTIME_NAMEThe runtime of the running function.
APPWRITE_FUNCTION_RUNTIME_VERSIONThe runtime version of the running function.

Learn to add variables to you function

You can access the environment variables through the systems library of each language.

export default async ({ req, res, log }) => {
    return res.send(process​.env.MY_VAR);
}

Dependencies

To install your dependencies before your function is built, you should add the relevant install command to the top your function's Build setting > Commands. You can find this setting under Functions > your function > Settings > Configuration > Build settings.

Make sure to include dependency files like package.json, composer.json, requirements.txt, etc. in your function's configured root directory. Do not include the dependency folders like node_modules, vendor, etc. in your function's root directory. The dependencies installed for your local OS may not work in the executor environments

Your function's dependencies should be managed by the package manager of each language. By default, we include the following package managers in each runtime.

 LanguagePackage ManagerCommands
Node.js logo Node.js logoNode.jsNPMnpm install
PHP logo PHP logoPHPComposercomposer install
Python logo Python logoPythonpippip install -r requirements.txt
Ruby logo Ruby logoRubyBundlerbundle install
Deno logo Deno logoDenodenodeno cache <ENTRYPOINT_FILE>
Dart logo Dart logoDartpubpub get
Swift logo Swift logoSwiftSwift Package Managerswift package resolve
.NET logo .NET logo.NETNuGetdotnet restore
Bun logo Bun logoBunbunbun install
Kotlin logo Kotlin logoKotlinGradleN/A
Java logo Java logoJavaGradleN/A
C++ logo C++ logoC++NoneN/A

Using Appwrite in a function

Appwrite can be used in your functions by adding the relevant SDK to your function's dependencies. Authenticating with Appwrite is done via an API key or a JWT token. API keys must be generated and exported as an environment variable.

You can read more about authentication in the JWT login section of the docs.

Using with API key

API keys have defined scopes when you create them. They ignore permissions and operate without a sessions. Use API keys if the function should act as an admin type role, instead of acting on behalf of a user. Pass in your API key as an environment variable. Never share API keys with users.

import { Client, Databases, ID } from 'node-appwrite';

export default async ({ req, res, log, error }) => {

    const client = new Client()
        .setEndpoint('https://cloud.appwrite.io/v1')
        .setProject(process​.env.APPWRITE_FUNCTION_PROJECT_ID)
        .setKey(process​.env.APPWRITE_API_KEY);

    const databases = new Databases(client);

    try {
        await databases.createDocument(
            '<DATABASE_ID>',
            '[COLLECTION_ID]',
            ID.unique(),
            {}
        )
    } catch (e) {
        error("Failed to create document: " + e.message)
        return res.send("Failed to create document")
    }

    return res.send("Document created")
}

Using with JWT

JWTs allow you to act on behalf of an user in your Appwrite Function. When using JWTs, you will be able to access and change only the resources with the same permissions as the user account that signed the JWT. This preserves the permissions you configured on each resource.

If the Appwrite Function is invoked by an authenticated user, the x-appwrite-user-jwt header is automatically passed in.

import { Client, Databases, ID } from 'node-appwrite';

export default async ({ req, res, log }) => {
    const client = new Client()
        .setEndpoint('https://cloud.appwrite.io/v1')
        .setProject(process​.env.APPWRITE_FUNCTION_PROJECT_ID)

    if (req.headers['x-appwrite-user-jwt']) {
        client.setJWT(req.headers['x-appwrite-user-jwt'])
    } else {
        return res.send("Please sign in, JWT not found")
    }

    const databases = new Databases(client);

    try {
        await databases.createDocument(
            '<DATABASE_ID>',
            '[COLLECTION_ID]',
            ID.unique(),
            {}
        )
    } catch (e) {
        log("Failed to create document: " + e.message)
        return res.send("Failed to create document")
    }

    return res.send("Document created")
}

Code structure

As your functions grow, you may find yourself needing to split your code into multiple files. This helps you keep your codebase maintainable and easy to read. Here's how you can accomplish code splitting.