Skip to main content
  1. Blogs/

Building cloud functions with appwrite

·783 words·4 mins
Tech Builds appwrite backend selenium browserless cloud functions

If you’re anything like me a few months ago, you’re probably wondering what the heck is appwrite? Well fear not, in this article I will walk you through probably one of the most promising tools I have used this year.

Introduction #

Appwrite is a platform that provides common backend services in the form of API’s that you can then use in your application. This way, the process of getting your backend up and running is greatly reduced and made easier. Thus allowing you to focus on building functionality. You can have a fully functioning user management system with one call to the api 🤯.

Appwrite logo

As if that’s not awesome enough, unlike most platforms of this nature like firebase, you can self host this platform on a server of your choosing or opt to use the cloud version provided by appwrite. This is largely due to the fact that the platform is open source and enjoys a large and strong community.

What we will be building #

To get started with appwrite we will build a python cloud function that takes screenshots of websites and saves them on appwrite storage and database.

This is a simple, yet complex function that will give us a glimpse of just how much appwrite can do.

Getting started #

To get started head over to appwrite.io and create an account. You will now have access to the cloud console. In the console create a new project; add a platform to the project in our case we will add the Web App since our application is a web application. Note that in this step you will need to set your hostname as * since you would like to test your application from a local instance.

In the console, you can create databases, collections for the database, storage buckets, users and functions.

That’s it you have a functional backend that’s up and running 🎉!

Appwrite functions #

To begin we will need to create an appwrite cloud function to capture our screenshots and store them in our application. We do this by heading to the functions tab on our dashboard and selecting Create function. Give the function an appropriate name and select a runtime, in our case we select Python 3.9. We do this so that we can leverage the Selenium library which is a powerful tool in web automation.

Skip through all the other settings (we’ll get back to them). You should now have access to a deployment option. This option allows you to upload a python script that will then be executed.

You can find the complete code to the function here. The code contains a main.py file and a requirements.txt file. The key functionality being in the main.py file. This file contains the screenshot capture logic and the appwrite saving logic.

Appwrite client initialization #

# Initialize Appwrite client
client = Client()
client.set_endpoint('https://cloud.appwrite.io/v1')
client.set_project(req.variables.get('project_id',None))
client.set_key(req.variables.get('api_key',None))

In this snippet we initialize the appwrite client and pass in some variables that are auto-magically available to your function from you console. To set these variables head over to the setting tab in your function and add the desired variables. Note: the values of these variables are available to you in the console eg. the project id and api key.

Appwrite storage #

# Upload the screenshot to Appwrite storage
storage = Storage(client)
file_info = storage.create_file(req.variables.get('bucket_id',None), str(ID.custom(uuid.uuid4())), InputFile.from_bytes(website['bytes'], "screenshot.png"))

Now to store the screenshot, simply create a storage object and provide it the client instance. To create the file call create_file method and provide your bucket_id, uniqueID,file bytes, and filename.

Check out our functions available to manipulate the storage here.

Appwrite database #

# Store the URL in Appwrite database
database = Databases(client)
database.create_document(req.variables.get('database_id',None), req.variables.get('collection_id',None), str(ID.custom(uuid.uuid4())), data)

To store a reference to the database, we create a database object and pass the client instance. We can now use the create_document function to create a document in our database. You will need the database_id,collection_id, a unique ID and data.

Check out our functions available to manipulate the database here.

Deployment #

To deploy the function simply click create deployment and choose the manual option. Provide a tar.gz compressed folder of the main.py and requirements.txt files above. Appwrite will setup the environment and build your project appropriately.

You can then choose to execute your code manually by clicking execute or setup a cron job in the settings panel. Note: Alternatively you could call this function in your frontend code using the appwrite API.

Conclusion #

That’s it folks you have a backend application with just a few lines of code. Imagine the amount of time you could save with this? Mybe you can finally finish that pet project you’ve been procrastinating 👀.