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.
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 -y
Install the undici
library. This library includes a fetch
function that you can use to make HTTP requests.
npm install undici
Finally, 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 -y
Install the guzzlehttp/guzzle
library. This library includes a get
function that you can use to make HTTP requests.
composer require guzzlehttp/guzzle
Finally, 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.txt
Install the requests
library. This library includes a get
function that you can use to make HTTP requests.
echo "requests" >> requirements.txt
pip install -r requirements.txt
Finally, 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_function
description: Appwrite Function
version: 1.0.0
environment:
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 http
Finally, 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'" >> Gemfile
bundle install
Finally, add bundle install
to your function's build commands in the Appwrite Console.
Code
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 | string | The name of the topic |
description | string | Long form description of the topic |
Create a Votes table with the following columns:
Name | Type | Description |
userId | string | The ID of the user who cast the vote |
topicId | string | The ID of the topic that was voted on |
vote | string | The vote cast by the user. Must be either "yes" or "no" |
Code
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.
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 | string | The name of the message author |
email | string | The email of the message author |
content | string | The content of the message |
Code
Use the function by navigating to the function URL in the browser. Submit the form to store the message in the table.