Make your Backend development more productive with AppWrite

Β·

17 min read

Make your Backend development more productive with AppWrite

1. Introduction to AppWrite πŸ“š

Appwrite is a secure, open-source backend that abstracts the complexity of common, complex, and repetitive tasks required for building a modern app. Appwrite provides you with a set of secure APIs, tools, and a management console UI to help you build your apps quickly without compromising security. Use Appwrite to add user authentication and management, data and file storage, run server-side code, image manipulation, localization, and more into your apps.

Every website is a unique project with unique requirements. Appwrite adapts to unique habits and needs. You can integrate Appwrite directly with the client app or use it alongside your existing backend server, using the language and technologies that the user wants to use.

2. Appwrite CLI </>

The Appwrite CLI is a command-line application that allows you to interact with the Appwrite server and perform server-side tasks using your terminal. This includes creating and managing projects, managing resources (documents, files, users), creating and deploying Appwrite Functions, and other operations available through Appwrite's API.

  • Deployment

    The Apprite CLI allows you to create and deploy databases, collections, buckets, teams and functions to your Appwrite project from a configuration file. This is especially helpful if you're trying to track project setup using version control.

  • Initializing Your Project

    After you're logged in, the CLI needs to be initialized with your Appwrite project. You can initialize the CLI using:

      appwrite init project
    

    You can fetch all the existing databases and collections in your current project using:

      appwrite init collection
    

    You can fetch all the existing teams in your current project using:

      appwrite init team
    
    • Deploying Appwrite Functions

      The CLI also handles the creation and deployment of Appwrite Functions. You can initialize a new function using:

        appwrite init function
        ? What would you like to name your function? My Awesome Function
        ? What runtime would you like to use? Node.js (node-15.5)
        βœ“ Success
      

This command creates a new function My Awesome Function in your current Appwrite project and also creates a template function for you to get started. You can now deploy this function using:

    appwrite deploy function
    ? Which functions would you like to deploy? Awesome Function (621229798628cf5bf712)
    β„Ή Info Deploying function Awesome Function ( 621229798628cf5bf712 )
    βœ“ Success Deployed Awesome Function ( 621229798628cf5bf712 )
  • Deploying Databases and Collections

    The Appwrite CLI also helps you migrate your project's databases and collections from a development server to a production server. You can deploy all the databases and collections in your appwrite.json file using:

      appwrite deploy collection
    
  • Deploying Teams

The Appwrite CLI can create teams to organize users. Teams can be used to grant access permissions to a group of users.

appwrite deploy team
  • Deploying Storage Buckets

    The Appwrite CLI allows you to configure and deploy buckets across projects. All the bucket's settings are available through the appwrite.json file.

      appwrite deploy bucket
    
  • Commands

    Other than commands to create and deploy databases, collections, functions, teams, and buckets, the Appwrite CLI can be used as a Server SDK as well. The Appwrite CLI has a command for every Server API endpoint.

    Commands follows the following general syntax:

appwrite [COMMAND] --[OPTIONS]
  • Create User

    To create a new user in your project, you can use the create command.

      appwrite users create --userId "unique()" \
          --email hello@appwrite.io \
          --password very_strong_password
    
  • List Users

    To get a list of all your project users, you can use the list command.

      appwrite users list
    

    In case of errors with any command, you can get more information about what went wrong using the --verbose flag

      appwrite users list --verbose
    
  • List Collections

    To get a list of all your collections, you can use the listCollections command.

      appwrite databases listCollections --databaseId [DATABASE_ID]
    

    If you wish to parse the output from the CLI, you can request the CLI output in JSON format using the --json flag

      appwrite databases listCollections --databaseId [DATABASE_ID]--json
    
  • Get a Collection

    To get more information on a particular collection, you can make use of the getCollection command and pass in the collectionId.

      appwrite databases getCollection --databaseId [DATABASE_ID] --collectionId [COLLECTION_ID]
    
  • Create Document

    To create a new document in an existing collection, use the createDocument command.

      appwrite databases createDocument \
          --databaseId [DATABASE_ID] --collectionId [COLLECTION_ID] \
          --documentId 'unique()' --data '{ "Name": "Iron Man" }' \
          --permissions 'read("any")' 'write("team:abc")'
    
  • CI Mode

    The Appwrite CLI can be used in a non-interactive and headless manner, without saving configuration or sessions. This is especially useful when you want to automate tasks on a CI server.

    You can enable CI mode for the Appwrite CLI by setting the project ID, endpoint, and API Key:

appwrite client \
    --endpoint https://cloud.appwrite.io/v1 \
    --projectId [YOUR_PROJECT_ID] \
    --key YOUR_API_KEY
  • When you set the global configuration parameters using the appwrite client command, they take precedence over the local configuration parameters in your appwrite.json thereby switching the CLI to non-interactive mode.

    • Non-interactive Deployment

      Appwrite's deploy commands can also be executed in a non-interactive mode. This applies to both function and collection deployment.

      You can deploy a function non-interactively by using the --yes option to skip all warnings and specifying which functions you want to deploy.

  • Deploy a single function by ID:

      appwrite deploy function --functionId [FUNCTION ID] --yes
    
  • Deploy all functions:

      appwrite deploy function --all --yes
    

    You can deploy databases, collections, teams, and buckets non-interactively in a similar way by using the --all and --yes option.

  • Deploy all databases and collections:

      appwrite deploy collection --all --yes
    
  • Deploy all teams:

      appwrite deploy team --all --yes
    
  • Deploy all buckets:

      appwrite deploy buckets --all --yes
    

3. Support with SDK πŸ‘©β€πŸ’»

  • Client SDKs

    Client libraries for integrating with Appwrite to build client-based applications and websites.

  • Server SDKs

    Libraries for integrating with Appwrite to build server-side integrations

  • Integrate without an SDK

    We are always looking to add new SDKs to our platform, but if the SDK you are looking for is still missing or in a beta channel, you can always integrate with Appwrite directly using any standard HTTP client and the Appwrite REST API.

4. APIs Support πŸ› 

  • REST

    The REST API allows you to access your Appwrite server through HTTP requests without needing an SDK. Each endpoint in the API represents a specific operation on a specific resource

    Read More

  • GraphQL

    The GraphQL API allows you to query and mutate any resource type on your Appwrite server through the endpoint /v1/graphql. Every endpoint available through REST is available through GraphQL, except for OAuth.

    Read More

  • Realtime

    Realtime allows you to listen to any events on the server side in realtime using the subscribe method.

    Read More

5. Core Components of Appwrite

  • User Authentication and Management

    Appwrite offers built-in user authentication and management services, supporting multiple authentication methods such as email and password, OAuth2 providers (e.g., Google, Facebook), and more. Appwrite also provides APIs for user registration, login, password recovery, and session management.

  • Database

    Appwrite's database system is designed with flexibility in mind, allowing developers to define their structured collections of documents. The database offers features like data validation, indexing, and real-time data updates to ensure efficient data management. With Appwrite's role-based access control, you can define granular permissions to control data access and modification.

  • Storage

    Appwrite provides a secure and scalable storage solution for your app's files, such as images, documents, and videos. It offers APIs for file upload, download, and management, along with support for on-the-fly file transformations like image resizing and cropping.

  • Functions

    Appwrite's serverless functions enable developers to create, deploy, and manage custom backend logic without the hassle of managing servers. Functions can be written in multiple languages, including Node.js, Python, and more. Appwrite functions can be triggered by various events, such as database operations or user actions, allowing for flexible and efficient workflows.

  • Real-time Events

    Real-time events in Appwrite allow developers to create responsive and interactive applications by subscribing to and reacting to events as they happen. This feature is particularly useful for applications requiring real-time communication or live updates, such as chat applications or collaborative editing tools.

6. Appwrite services

  • Users API

    The user service is used to manage the users in your project. It is used to search for and block users, as well as to view your user information, current section, and most recent activity. You can also update your user's preferences and personal information through your user service.

  • Databases API

    This service enables you to create structured document collections, query and filter document lists, and manage an advanced set of read and write access permissions.

  • Storage API

    This service enables you to manage your project files by uploading, viewing, downloading, and querying all of your project files. Files are managed in this service using buckets. Collections in the databases service are similar to storage buckets. The difference is that buckets have more power to decide what types of files and sizes you want to allow in that bucket, as well as whether or not to encrypt the files, use antivirus, and much more.

  • Localization API

    This service enables you to modify your app dashboard based on the location of your users. You can get your user's location, IP address, list of countries and continent names, phone codes, currencies, and more by using this service.

  • Functions API

    This service aids in the discovery of custom behavior that can be triggered by any supported Appwrite system event or a predefined schedule.

  • Avatars API

    The service aims to help you complete your daily tasks that are related to your app image, icons, and avatars.

  • Health API

    You can use this service to validate and monitor your Appwrite server instance, ensuring that all of its internal components are up and running and responsive.

  • Account API

    You can use this service to manage and authenticate a user account. It is also used to update user information, retrieve user sections across multiple devices, and retrieve user security logs containing recent activity. You can use the account service to register new user accounts, using the Create Account, Create Magic URL session, or Create Phone session endpoint. You can authenticate the user account using any of the available signing methods. Once a user has been authenticated, a new session object is created to allow the user access to his or her private data and settings.
    One of the account service's fantastic features is the multiple sign-in methods, which allow you to authenticate a user. Following authentication, a new session object will be created to allow the user access to his or her private data or settings.

  • Teams API

    The team service enables you to group project users and give them read and write access to project resources such as database documents or storage files.
    When a user creates a team, he or she becomes the team owner and can exercise ownership by inviting a new team member. Only team owners can invite users.
    You will go through a series of team service sessions that will help you create a team in Appwrite.

  • Create Team

    When a user creates a team, he or she becomes the team owner, and only the owner can invite new members, add new owners, update the team, and delete the team.
    Here's an example of how to use the Create Team Endpoint.

      import { Client, Teams } from "appwrite";
    
      const client = new Client();
    
      const teams = new Teams(client);
    
      client
          .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint
          .setProject('5df5acd0d48c2') // Your project ID
      ;
    
      const promise = teams.create('[TEAM_ID]', '[NAME]');
    
      promise.then(function (response) {
          console.log(response); // Success
      }, function (error) {
          console.log(error); // Failure
      });
    

  • List Teams

    This endpoint returns a list of all the teams that the current user is a member of. You can use the parameters to select your results. It also returns a list of all the latest project teams.
    The List Team Endpoint is demonstrated in the code below.

import { Client, Teams } from "appwrite";

const client = new Client();

const teams = new Teams(client);

client
    .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint
    .setProject('5df5acd0d48c2') // Your project ID
;

const promise = teams.list();

promise.then(function (response) {
    console.log(response); // Success
}, function (error) {
    console.log(error); // Failure
});
  • Get Team

    You can get a team by its ID using this Endpoint. This resource is accessible for all team members to read.
    The Get Team Endpoint is demonstrated in the code below.

import { Client, Teams } from "appwrite";

const client = new Client();

const teams = new Teams(client);

client
    .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint
    .setProject('5df5acd0d48c2') // Your project ID
;

const promise = teams.get('[TEAM_ID]');

promise.then(function (response) {
    console.log(response); // Success
}, function (error) {
    console.log(error); // Failure
});
  • Update Team

    You can use this endpoint to update a team using its ID, and the update can be performed by members who have the owner role.
    The Update Team Endpoint is demonstrated in the code below.

import { Client, Teams } from "appwrite";

const client = new Client();

const teams = new Teams(client);

client
    .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint
    .setProject('5df5acd0d48c2') // Your project ID
;

const promise = teams.update('[TEAM_ID]', '[NAME]');

promise.then(function (response) {
    console.log(response); // Success
}, function (error) {
    console.log(error); // Failure
});

  • Delete Team

    You can use this endpoint to delete a team using its ID, and the update can be performed by members who have the owner role.
    The Delete Team Endpoint is demonstrated in the code below.

      import { Client, Teams } from "appwrite";
    
      const client = new Client();
    
      const teams = new Teams(client);
    
      client
          .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint
          .setProject('5df5acd0d48c2') // Your project ID
      ;
    
      const promise = teams.delete('[TEAM_ID]');
    
      promise.then(function (response) {
          console.log(response); // Success
      }, function (error) {
          console.log(error); // Failure
      });
    

7. Appwrite vs FireBase

Appwrite has a beautiful user interface, is simple to set up, and is consistent across platforms. Firebase is expensive, has limited scalability, is not open source, relies on a third party, cannot filter queries, and is not flexible.

8. Installation βš™

Appwrite is designed to run in a containerized environment. Running your server is as easy as running one command from your terminal. You can either run Appwrite on your localhost using docker-compose or on any other container orchestration tool, such as Kubernetes, Docker Swarm, or Rancher.

The easiest way to start running your Appwrite server is by running our docker-compose file. Before running the installation command, make sure you have Docker installed on your machine.

Appwrite is designed to run well on both small and large deployments. The minimum requirements to run Appwrite are as little as 1 CPU core and 2GB of RAM, and an operating system that supports Docker. Appwrite requires Docker Compose Version 2. To install Appwrite, make sure your Docker installation is updated to support Composer V2.

You will be prompted to configure the following during the setup command:

  • Your Appwrite instance's HTTP and HTTPS ports.

  • Your Appwrite instance's secret key which used to encrypt sensitive data.

  • Your Appwrite instance's main hostname. Appwrite will generate a certificate using this hostname.

  • Your Appwrite instance's DNS A record hostname. Typically set to the same value as your Appwrite instance's hostname.

  • Unix

  • Windows

    Hyper-V and Containers Windows features must be enabled to run Appwrite on Windows with Docker. If you don't have these features available, you can install Docker Desktop which uses Virtualbox to run Appwrite on a Virtual Machine.

    • CMD

    • Powershell

  • Mac OS

    • Step -1: Download the docker desktop for Mac here: https://docs.docker.com/desktop/mac/install/

    • Step-2: Once downloaded, open the file and drag the docker icon to the applications folder as shown below

    • Step-3: Run the app. It will ask for permission to install tools (including docker cli which is needed for running appwrite)

    • Step-4: You can verify the docker cli installation by typing the following in your terminal:

        docker version
      
    • Step-5: Installing Appwrite

      Appwrite offers easy and simple installation by directly running their docker installer tool from the terminal. Now that docker cli is set up and verified by following steps mentioned above, run the following command in your terminal to install appwrite:

        docker run -it --rm \
            --volume /var/run/docker.sock:/var/run/docker.sock \
            --volume "$(pwd)"/appwrite:/usr/src/code/appwrite:rw \
            --entrypoint="install" \
            appwrite/appwrite:0.11.0
      

It will ask you to define the following parameters:

  • HTTP port

  • HTTPS port

  • API key

  • Hostname

  • Custom domain

You can either customize or choose defaults for the above parameters.

After you have specified these details, it will take a few moments. If everything went fine, you will get a message saying Appwrite installed successfully

You can now access your appwrite dashboard by visiting http://<hostname or custom domain>:<HTTP port>
It will look like this:

9. Benefits Of using AppWrite 🎯

  • Simplified Backend Development:

    Appwrite takes care of managing backend infrastructure, including database, storage, and authentication, allowing developers to focus on building front-end features and user experiences.

  • Cross-Platform Compatibility:

    Appwrite provides SDKs for popular platforms such as JavaScript, Flutter, and more, ensuring seamless integration with your preferred development framework.

  • Scalability:

    Appwrite is built on Docker, a containerization platform that enables easy scaling to meet the demands of growing applications.

  • Security:

    Appwrite prioritizes security, offering features such as built-in user authentication, role-based access control, and data validation to protect your app and user data.

  • Open Source:

    As an open-source platform, Appwrite offers developers the freedom to customize, contribute, and collaborate on the project to suit their unique requirements.

10. Overall Architecture 🧩

Appwrite uses a microservices architecture that was designed for easy scaling and delegation of responsibilities. In addition, Appwrite supports multiple APIs, such as REST, WebSocket, and GraphQL to allow you to interact with your resources by leveraging your existing knowledge and protocols of choice.

The Appwrite API layer was designed to be extremely fast by leveraging in-memory caching and delegating any heavy-lifting tasks to the Appwrite background workers. The background workers also allow you to precisely control your computing capacity and costs using a message queue to handle the load.

11. AppWrite Integration with Cloud Platformsβ˜πŸ”©

12. Getting Started with AppWrite πŸš€

  • Creating your first project

    In your browser, type in the domain name you have set up or the IP address of your droplet, and you should be presented with a login or signup page. You can get to the signup page by clicking on the Sign Up button.

    Provide your details and signup. The account you create will be the root account for managing your Appwrite server. Once you signup, you will be redirected and presented with a dialog to create your first Project. Enter the name and optional custom ID and click Create project button.

    You will then be redirected to the project overview page, where you can set up your project further to connect with your applications, set up databases, storage and more:

  • Appwrite can be installed in two ways. You can use the one-click installation method to get Appwrite up and running on DigitalOcean or Gitpod pretty quickly.

    And another method is that You will be installing Appwrite using Docker.

    Below we saw an example of how to access appwrite using Docker.

    • Confirm that you have installed Docker and that it is running.

    • Go to Appwrite.io.

    • Click on the Get Started button.

    • Copy the following command(Appwrite server is stored as a Docker container, which you can quickly set up from your terminal using the docker-compose command).

        docker run -it --rm \
            --volume /var/run/docker.sock:/var/run/docker.sock \
            --volume "$(pwd)"/appwrite:/usr/src/code/appwrite:rw \
            --entrypoint="install" \
            appwrite/appwrite:1.0.3
      
    • Open your terminal and paste it there.

    • Press the enter key.

    • During installation, you will be asked where you want to run your HTTP port, as shown below.

    • You can make port 80 your default. - You will then be asked to select a secret API, which can be your name, as shown below.

    • You will be prompted to enter your Appwrite hostname, which should be the local host.

    • You will be prompted to enter your DNS record; continue to use the hostname for this.

    • Press the enter key and wait for it to be fully installed; you should see the message Appwrite installed successfully once the installation is complete.

    • If you go to your Docker, you should see your Appwrite container running, which has 20 other containers, as shown below.

    • To sign in to Appwrite, open your browser and type

        localhost:80
      
    • Then enter your information as shown below.

    • If you do not already have an Appwrite account, you should create one before you can log in. After signing into Appwrite, you'll be able to see your dashboard and create a new project, as shown below.

13. Contribute & Get involved with AppWrite community πŸ’πŸ»β€β™‚οΈπŸ’πŸ»β€β™€οΈ

14. Resources πŸ“‘πŸ“œ

🚩 https://appwrite.io/

🚩 https://appwrite.io/docs

🚩 https://youtu.be/eqbz7PYNfHg

🚩 https://youtu.be/pk92hS_d_ns

🚩 https://www.youtube.com/live/jt5CGCPCsEc?feature=share

🚩 https://cloud.appwrite.io/login

That's all for this blog, I hope you will learn something new. And feel free to share your thoughts and feedback, Thanks for reading.

Feel free to reach out to me πŸ‘€

Twitter πŸ–±

LinkedIn πŸ–±

Github πŸ–±

Did you find this article valuable?

Support Hashnode by becoming a sponsor. Any amount is appreciated!

Β