DEV Community

loading...
Cover image for #30DaysOfAppwrite : Server Side SDKs
Appwrite

#30DaysOfAppwrite : Server Side SDKs

christyjacob4 profile image Christy Jacob ・5 min read

Intro

Appwrite is an open-source, self-hosted Backend-as-a-Service that makes app development easier with a suite of SDKs and APIs to accelerate app development. #30DaysOfAppwrite is a month long event focussed at giving developers a walkthrough of all of Appwrite's features, starting from the basics to more advanced features like cloud functions! Alongside we will also be building a fully featured Medium Clone to demonstrate how these concepts can be applied when building a real world app. We also have some exciting prizes for developers who follow along with us!

Server Side SDKs

Welcome to Day 7 👋 . Today we're going to take a look at Appwrite's Server Side SDKs and talk about the differences between the Client and Server SDKs. The differences between Client and Server Side SDKs seem to be mysterious for a lot of devs so this guide will aim to demystify some of those confusions.

Appwrite's vision emphasizes the fact that Backend As A Service should not be designed only for front-end developers. Building upon this vision, Appwrite was designed to be platform agnostic and integrates seamlessly with client and server side applications. Because Appwrite is self-hosted it can be used behind your existing firewall and work alongside your existing backend services. Appwrite doesn't aim to replace your backend, instead work alongside it.

Appwrite officially supports 6 Server Side SDKs with more in the pipeline. If you didn't already know, all our SDKs are automatically generated from the Swagger Specification of our APIs. This allows our small team to maintain a total of 8 (Client + Server) SDKs. We just ❤️ PRs! If you would like to help us create SDKs in your favourite language, feel free to check out SDK Generator.

🤔 How are they different?

Authentication

The key difference between the client and server-side SDKs is the authentication mechanism. Server side SDKs use a scoped API key to access the Appwrite API whereas the client side SDKs rely on a secure cookie for authentication.

Scopes

The second main difference is the scopes that the client and server side SDKs are allowed to access. The scopes limit the kind of tasks you can achieve with the SDKs. The server side SDKs offer much more power and flexibility and allows you to control many more aspects of Appwrite. Using your API Keys, you can access Appwrite services using the SDK of your choice.

To create a new API key, go to your API keys tab in your project setting using your Appwrite console and click the 'Add API Key' button. When adding a new API Key, you can choose the scopes that you would like to grant to your application. It is a best practice to allow only the permissions you need to meet your project goals. If you need to replace your API Key, create a new key, update your app credentials and, once ready, delete your old key.

When using Appwrite API from your Server Side with an API Key you will automatically run in admin mode. Admin mode disables the default user permission access control restrictions and allows you to access all the server resources ( Documents, Users, Collections, Files, Teams) in your project, regardless of the read and write permissions. This is very useful when you want to manipulate your users' data like files and documents or even if you want to get a list of your users.

It is not recommended to run admin mode from a client as it will lead to huge privacy and security risks. Check the Admin Mode documentation to learn more.

The following table is a good visualisation of what you can and cannot do with the Client and Server Side SDKs and is a good summary of what we've covered.

Name Description Server Client
account Access to read and write on behalf of the currently logged-in user
users.read Access to read your project's users
users.write Access to create, update, and delete your project's users
teams.read Access to read your project's teams
teams.write Access to create, update, and delete your project's teams
collections.read Access to read your project's database collections
collections.write Access to create, update, and delete your project's database collections
documents.read Access to read your project's database documents
documents.write Access to create, update, and delete your project's database documents
files.read Access to read your project's storage files and preview images
files.write Access to create, update, and delete your project's storage files
functions.read Access to read your project's functions and code tags
functions.write Access to create, update, and delete your project's functions and code tags
execution.read Access to read your project's execution logs
execution.write Access to execute your project's functions
locale.read Access to access your project's Locale service
avatars.read Access to access your project's Avatars service
health.read Access to read your project's health status

Getting started

Getting started with the server Side SDK and making your first request is really simple. For the sake of this example, we will choose the Node SDK - the same principles apply to all the other SDKs as well.

The first step is to create a Node project and install the node-appwrite package.

$ mkdir getting-started
$ cd getting-started
$ npm init -y
$ npm install node-appwrite --save
Enter fullscreen mode Exit fullscreen mode

The next step is to head to your Appwrite Dashboard and create a new project. Give your project a name and click Create to get started. Once the project is created, head over to the API keys section and create a key with the required scopes (Make sure it has the users.read and users.write scopes since the example depends on that). Copy this key as we will need it in the next step. Also take a note of your Project ID and API Endpoint which can be found under the Settings section in your Appwrite Dashboard.
Settings

It's time to initialise your SDK and make your first request. Fill in all the values you copied in the previous step. We will then try to create a user using the Appwrite SDK.

const sdk = require('node-appwrite');

let client = new sdk.Client();

client
    .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint
    .setProject('5df5acd0d48c2') // Your project ID
    .setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret key
;

let users = new sdk.Users(client);

let promise = users.create('email@example.com', 'password');

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

Enter fullscreen mode Exit fullscreen mode

There you have it! That was your first request using Appwrite's server-side SDK! If you would like to see this example in other languages that we support, you can check them out here.

If you feel adventurous and would like to use the Appwrite API using your favourite HTTP request library, this guide was written exactly for that!

Credits

We hope you liked this write up. You can follow #30DaysOfAppwrite on Social Media to keep up with all of our posts. The complete event timeline can be found here

Feel free to reach out to us on Discord if you would like to learn more about Appwrite, Aliens or Unicorns 🦄. Stay tuned for tomorrow's article! Until then 👋

Discussion (0)

Forem Open with the Forem app