DEV Community

Alex Spinov
Alex Spinov

Posted on

Appwrite Functions Has a Free API: Run Serverless Code with Any Language on Your Own Server

What is Appwrite Functions?

Appwrite is an open-source Backend-as-a-Service, and Appwrite Functions lets you run serverless functions in Node.js, Python, PHP, Ruby, Dart, Swift, Kotlin, Java, .NET, Bun, Deno — self-hosted or on Appwrite Cloud.

Free tier: 750K executions/month.

Quick Start

npm install -g appwrite-cli
appwrite login
appwrite init function
Enter fullscreen mode Exit fullscreen mode

Create a Function

// src/main.js
export default async ({ req, res, log, error }) => {
  if (req.method === "GET") {
    const name = req.query.name || "World";
    return res.json({ message: `Hello ${name}!` });
  }

  if (req.method === "POST") {
    const body = JSON.parse(req.body);
    log(`Received: ${JSON.stringify(body)}`);

    // Process data
    const result = await processOrder(body);
    return res.json(result);
  }

  return res.json({ error: "Method not allowed" }, 405);
};
Enter fullscreen mode Exit fullscreen mode

Deploy

appwrite deploy function
Enter fullscreen mode Exit fullscreen mode

The REST API

export APPWRITE_URL="https://cloud.appwrite.io/v1"
export APPWRITE_KEY="your-api-key"
export PROJECT_ID="your-project-id"
Enter fullscreen mode Exit fullscreen mode

Execute Function

curl -X POST "$APPWRITE_URL/functions/FUNCTION_ID/executions" \
  -H "X-Appwrite-Project: $PROJECT_ID" \
  -H "X-Appwrite-Key: $APPWRITE_KEY" \
  -H "Content-Type: application/json" \
  -d '{"body": "{\"name\": \"Alice\"}"}'
Enter fullscreen mode Exit fullscreen mode

List Executions

curl -s "$APPWRITE_URL/functions/FUNCTION_ID/executions" \
  -H "X-Appwrite-Project: $PROJECT_ID" \
  -H "X-Appwrite-Key: $APPWRITE_KEY" | jq '.executions[] | {status, duration, responseBody}'
Enter fullscreen mode Exit fullscreen mode

Manage Functions

# List functions
curl -s "$APPWRITE_URL/functions" \
  -H "X-Appwrite-Project: $PROJECT_ID" \
  -H "X-Appwrite-Key: $APPWRITE_KEY" | jq '.functions[] | {name, runtime, status}'

# Create function
curl -X POST "$APPWRITE_URL/functions" \
  -H "X-Appwrite-Project: $PROJECT_ID" \
  -H "X-Appwrite-Key: $APPWRITE_KEY" \
  -d '{
    "functionId": "unique()",
    "name": "Process Orders",
    "runtime": "node-20.0",
    "execute": ["any"],
    "events": ["databases.*.collections.*.documents.*.create"],
    "schedule": "0 */6 * * *"
  }'
Enter fullscreen mode Exit fullscreen mode

Event Triggers

// Triggered when a document is created
export default async ({ req, res, log }) => {
  const event = req.headers["x-appwrite-event"];
  const payload = JSON.parse(req.body);

  if (event.includes("documents.create")) {
    log(`New document: ${payload.$id}`);
    // Send notification, process data, etc.
    await sendWelcomeEmail(payload.email);
  }

  return res.empty();
};
Enter fullscreen mode Exit fullscreen mode

Scheduled Functions (Cron)

# Run every hour
curl -X PATCH "$APPWRITE_URL/functions/FUNCTION_ID" \
  -H "X-Appwrite-Project: $PROJECT_ID" \
  -H "X-Appwrite-Key: $APPWRITE_KEY" \
  -d '{"schedule": "0 * * * *"}'
Enter fullscreen mode Exit fullscreen mode

Node.js SDK

import { Client, Functions } from "node-appwrite";

const client = new Client()
  .setEndpoint("https://cloud.appwrite.io/v1")
  .setProject("PROJECT_ID")
  .setKey("API_KEY");

const functions = new Functions(client);

// Execute
const execution = await functions.createExecution(
  "FUNCTION_ID",
  JSON.stringify({ action: "process" }),
  false, // async
  "/api/process", // path
  "POST" // method
);

console.log(execution.responseBody);
Enter fullscreen mode Exit fullscreen mode

Supported Runtimes

Runtime Versions
Node.js 16, 18, 20, 21
Python 3.8 - 3.12
PHP 8.0 - 8.3
Ruby 3.1 - 3.3
Dart 2.x, 3.x
Bun 1.x
Deno 1.x
Kotlin 1.x
Swift 5.x
.NET 6.0, 7.0

Need serverless functions or backend automation?

📧 spinov001@gmail.com
🔧 My tools on Apify Store

AWS Lambda, Appwrite, or Cloudflare Workers? Share your pick!

Top comments (0)