Appwrite Functions
Appwrite Function Documentation
Functions
We split the functions into three different areas:
Init
- Which initializes the serverless function, handling the installs if using different libaries, ect…Core
- These are the notes for the core serverless function.Extra
- Additional notes and information regarding the expansion of a serverless function.
Init
Hello there, fellow developers! 🚀
Today, we’re diving deep into the world of cloud functions with a special focus on Appwrite - a wonderful platform that allows you to build secure web and mobile applications faster.
What is a Cloud Function?
At its core, a cloud function is a piece of code that runs in response to an event. You don’t need to manage the infrastructure; the cloud provider does that for you. Think of it as “Function as a Service” (FaaS). Cloud functions are a key part of the serverless architecture, allowing developers to focus on writing code without worrying about the underlying infrastructure.
Initializing Your Appwrite Cloud Function
-
Function Creation
:- Head over to your Appwrite Console, navigate to the Functions section, and click on
Add Function
. - Give your function a name and choose a runtime (e.g., Node.js, Python, Deno, etc.).
- Head over to your Appwrite Console, navigate to the Functions section, and click on
-
Code Your Function
:- Reference Core for the basics.
- Here is a basic / starter.
module.exports = async (req, res) => { const payload = req.payload || 'No payload provided. Add custom data when executing function.'; const secretKey = req.variables.SECRET_KEY || 'SECRET_KEY variable not found. You can set it in Function settings.'; const randomNumber = Math.random(); const trigger = req.variables.APPWRITE_FUNCTION_TRIGGER; res.json({ message: 'Hello from Appwrite!', payload, secretKey, randomNumber, trigger, }); };
-
Packaging
:- Cloud functions in Appwrite run inside Docker containers, so your function and any dependencies need to be packed into a tarball. For Node.js:
tar -czf code.tar.gz --exclude code.tar.gz .
- Cloud functions in Appwrite run inside Docker containers, so your function and any dependencies need to be packed into a tarball. For Node.js:
-
Deployment
:- Upload Your Code: Back in the Appwrite Console, under your function, click on the Deploy Tag button.
- Upload the code.tar.gz file.
- Set Your Triggers! Functions can respond to various events, like user registration or document creation. Select the triggers relevant to your use case.
- Execute! With everything set, hit the Execute button. Your function will run, and you can see the logs in real-time.
Yay! Congratulations, you’ve just deployed your first Appwrite Cloud Function! 🎉 The power of serverless lies in its simplicity and scalability. As your application grows, you can continue adding more functions without the hassle of managing servers or infrastructure.
I hope this introduction helps kickstart your journey with Appwrite Cloud Functions. Stay tuned for more in-depth tutorials, and happy coding! 🚀
Core
General documentation and information in reference to the cloud functions / open runtime for Appwrite.
Remember to import the SDK outside of the module function.
The main module.exports
is usually written like this
module.exports = async function (req, res) {}; // Passing req and res
The req
contains these three main objects:
req.headers
- An object with the request headers for the function.
req.payload
- An object with the payload, which is the main body data.
req.env
- An object with the environment data.
The res
can return two main objects:
res.send
- returns a text
and statusCode
.
res.json
- returns an object
and statusCode
.
Example of parsing the req.payload
check and res.json
return.
if (req.payload) {
try {
payload = JSON.parse(req.payload);
} catch (error) {
errorHandler('Corrupt payload for the function.');
}
} else {
errorHandler('Missing payload for the function.');
}
In this case our errorHandler
function would be:
const errorHandler = (__errorMessage, statusCode = 418) => {
res.json({ data: 'error', message: __errorMessage }, statusCode);
};
Extra
In the context of Appwrite Cloud Functions, “extra libraries” refer to external dependencies or packages that your function might need to execute correctly. Due to the isolated nature of cloud functions, they don’t inherently have access to all the libraries or tools available in a full-fledged server environment. To overcome this, Appwrite allows you to bundle these additional libraries with your function code. When you’re developing your function, you specify and package these libraries—be it npm packages for a Node.js function, pip packages for a Python function, or any other dependencies for other runtimes. Once bundled, Appwrite ensures that these libraries are available to your function at runtime, providing a seamless execution environment tailored to your specific needs.
PoC
- For data fetching, we recommend that you add an external module known as
axios
.const axios = require("axios").default;