Examples_
Accelerate your serverless development with Appwrite Functions examples. Access a library of code samples and use cases to jumpstart your projects.
4 min read
Appwrite Functions is all about flexibility. Behind the simple workflow hides some useful examples that can help you accomplish your goals faster. Take a look at the following.
1. Currency conversion API
Here's a currency conversion API that converts from Euros and Indian Rupees to US Dollars. We'll use an external API to get the latest exchange rates and query it using a dependency specific to each runtime.
Prerequisites
Run the following bash command to create a package.json file. This file is used to manage your Node.js project's dependencies.
npm init -yInstall the undici library. This library includes a fetch function that you can use to make HTTP requests.
npm install undiciFinally, add npm install to your function's build commands in the Appwrite Console.
Run the following bash command to create a composer.json file. This file is used to manage your PHP project's dependencies.
composer init -yInstall the guzzlehttp/guzzle library. This library includes a get function that you can use to make HTTP requests.
composer require guzzlehttp/guzzleFinally, add composer install to your function's build commands in the Appwrite Console.
Run the following bash command to create a requirements.txt file. This file is used to manage your Python project's dependencies.
touch requirements.txtInstall the requests library. This library includes a get function that you can use to make HTTP requests.
echo "requests" >> requirements.txtpip install -r requirements.txtFinally, add pip install -r requirements.txt to your function's build commands in the Appwrite Console.
Create a pubspec.yaml file with the following contents. This file is used to manage your Dart project's dependencies.
name: appwrite_functiondescription: Appwrite Functionversion: 1.0.0environment: sdk: '>=2.12.0 <3.0.0'Install the http library. This library includes a get function that you can use to make HTTP requests.
pub install httpFinally, add pub get to your function's build commands in the Appwrite Console.
Create a Gemfile file with the following contents. This file is used to manage your Ruby project's dependencies.
source 'https://rubygems.org'Install the httparty library. This library includes a get function that you can use to make HTTP requests.
echo "gem 'httparty'" >> Gemfilebundle installFinally, add bundle install to your function's build commands in the Appwrite Console.
Create a Cargo.toml file with the following contents. This file is used to manage your Rust project's dependencies.
[package]name = "handler"version = "0.1.0"edition = "2021"rust-version = "1.83"
[lib]name = "handler"path = "lib.rs"
[dependencies]openruntimes = { version = "1.0", package = "openruntimes-types-for-rust" }serde_json = "1.0"ureq = { version = "2.10", features = ["json"] }appwrite = "0.19"tokio = { version = "1", features = ["rt"] }urlencoding = "2.1"ureq is the blocking HTTP client used by the currency example. appwrite and tokio are used by the voting and contact form examples that call the Appwrite SDK. urlencoding is used by the contact form example to decode form fields. Cargo picks up Cargo.toml automatically, so no extra build command is required.
Code
import { fetch } from 'undici';
export default async function ({ req, res }) { if (req.path === '/eur') { const amountInEuros = Number(req.query.amount); const response = await fetch('https://api.exchangerate.host/latest?base=EUR&symbols=USD'); const data = await response.json(); const amountInDollars = amountInEuros * data.rates.USD; return res.text(amountInDollars.toString()); }
if (req.path === '/inr') { const amountInRupees = Number(req.query.amount); const response = await fetch('https://api.exchangerate.host/latest?base=INR&symbols=USD'); const data = await response.json(); const amountInDollars = amountInRupees * data.rates.USD; return res.text(amountInDollars.toString()); }
return res.text('Invalid path');};2. Voting system
Here's a simple voting system that allows users to vote on various topics. Appwrite Functions and the server SDK are used to enforce voting rules and prevent multiple votes from the same user for a single topic.
Prerequisites
Create a Topics table with the following columns:
| Name | Type | Description |
|---|---|---|
title | Varchar | The name of the topic |
description | Text | Long form description of the topic |
Create a Votes table with the following columns:
| Name | Type | Description |
|---|---|---|
userId | Varchar | The ID of the user who cast the vote |
topicId | Varchar | The ID of the topic that was voted on |
vote | Varchar | The vote cast by the user. Must be either "yes" or "no" |
Code
import { Client, TablesDB, Query, ID } from 'node-appwrite';
export default async function ({ req, res }) { const vote = { userId: req.query.userId, topicId: req.query.topicId, vote: req.query.vote };
if (vote.vote !== 'yes' && vote.vote !== 'no') { return res.json({ ok: false, message: 'You must vote yes or no.' }, 400); }
// Set project and set API key const client = new Client(); client .setProject(process.env.APPWRITE_FUNCTION_PROJECT_ID) .setKey(req.headers['x-appwrite-key']);
const tablesDB = new TablesDB(client);
const existingVotes = await tablesDB.listRows({ databaseId: '<DATABASE_ID>', tableId: '<VOTES_TABLE_ID>', queries: [ Query.equal('userId', vote.userId), Query.equal('topicId', vote.topicId) ] });
if (existingVotes.total > 0) { return res.json({ ok: false, message: 'You have already voted on this topic.' }, 400); }
const voteDocument = await tablesDB.createRow({ databaseId: '<DATABASE_ID>', tableId: '<VOTES_TABLE_ID>', rowId: ID.unique(), data: { userId: vote.userId, topicId: vote.topicId, vote: vote.vote, } });
return res.json({ ok: true, message: 'Vote cast.', vote: voteDocument });};Use the function by navigating to the function URL in the browser. The URL should contain the required parameters. For example, <YOUR_FUNCTION_URL>/?userId=<USER_ID>&topicId=<TOPIC_ID>&vote=yes to cast a vote.
3. HTML contact form
Here's a simple form page that handles form submissions, and can be used to store a user's message in a table. The form is submitted to the function using the POST method and the form data is sent as a URL-encoded string in the request body.
Prerequisites
Create a Messages table with the following columns:
| Name | Type | Description |
|---|---|---|
name | Varchar | The name of the message author |
email | The email of the message author | |
content | Text | The content of the message |
Code
import { Client, TablesDB, Query, ID } from 'node-appwrite';import querystring from 'node:querystring';
const html = `<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <title>Contact Form</title> </head> <body> <form action="/" method="POST"> <input type="text" id="name" name="name" placeholder="Name" required> <input type="email" id="email" name="email" placeholder="Email" required> <textarea id="content" name="content" placeholder="Message" required></textarea> <button type="submit">Submit</button> </form> </body></html>`
export default async function ({ req, res }) { if (req.method === 'GET') { return res.text(html, 200, {'content-type': 'text/html'}); }
if (req.method === 'POST' && req.headers['content-type'] === 'application/x-www-form-urlencoded') { const formData = querystring.parse(req.body);
const message = { name: formData.name, email: formData.email, content: formData.content };
// Set project and set API key const client = new Client() .setProject(process.env.APPWRITE_FUNCTION_PROJECT_ID) .setKey(req.headers['x-appwrite-key']);
const tablesDB = new TablesDB(client); const row = await tablesDB.createRow({ databaseId: '<DATABASE_ID>', tableId: '<MESSAGES_TABLE_ID>', rowId: ID.unique(), data: message });
return res.text("Message sent"); }
return res.text('Not found', 404);}Use the function by navigating to the function URL in the browser. Submit the form to store the message in the table.
Was this page helpful?
Share what worked or what we should fix. Once approved, our agents automatically apply suggested updates to the docs.