Skip to main content

Arianet BaaS quickstart

In this guide you create a Project, build a todos Table with RLS and a Policy, and send your first request with curl and JavaScript. It takes about five minutes.

1. Create a Project​

  1. In the panel, open Arianet BaaS and click Create new Project («ساخت Project جدید»).
  2. Project name («نام Project»): a recognisable name (Persian or English, up to 64 characters). You can change it later.
  3. Region: choose Tehran 1 («تهران ۱», thr-1).
  4. Choose a Plan («انتخاب Plan»): choose the Free Plan. The Plan's limits are listed on its card.
  5. Review and confirm («مرور و تأیید»): check the summary and click Create Project («ساخت Project»).

The Plan step of creating a Project

Creating the Database usually takes less than a minute. When you see "Project is ready" («Project آماده است»), the Endpoint is shown and you can click Create your first Table («ساخت اولین Table»).

Project is ready

2. Create a Table with RLS and a Policy​

  1. In the Project, open Table Editor and click New Table («Table جدید»).
  2. Name and security («نام و امنیت»): name it todos. Keep RLS on (it is on by default).
  3. Columns («ستون‌ها»): id (auto-incrementing) and created_at are already there. Add a title column of type text with Add column («افزودن ستون»), then click Owner column (user_id) («ستون مالک (user_id)») to create user_id: type uuid, default auth.uid() (the signed-in User's id), referencing the Auth Users so that deleting a User also deletes their Rows.
  4. Policies («Policyها»): tick the template "Each User only their own Rows" («هر User فقط Rowهای خودش») and pick the user_id column for it. If everyone should be able to read the Rows, also add "Public read, only signed-in Users write" («خواندن عمومی، نوشتن فقط User واردشده»).
  5. Review SQL («مرور SQL»): read the SQL that will run and click Create Table («ساخت Table»).

The Policies step of creating a Table

The SQL generated for "Each User only their own Rows" is:

create policy "todos: users manage own rows" on "public"."todos"
for all
to authenticated
using ((auth.uid() = user_id))
with check ((auth.uid() = user_id));

You add Rows in step 4 with a User's token; each Row's user_id becomes that User's id.

Do not turn RLS off

With RLS off, anyone who has your Anon Key can read and change every Row of the Table. The Anon Key ships in client-side code and is not a secret; RLS and Policies are what protect your data. Security and best practices

3. Copy the Endpoint and Anon Key​

Open API Keys and Connect («API Keys و اتصال»):

  • Endpoint is the Project's address, https://<ref>.baas.arianet.app.
  • Next to Anon Key, click Show («نمایش») and copy the key. It is hidden again after one minute.

API Keys and Connect

Below, replace <ref> with the Project's ref and <ANON_KEY> with this key.

4. First request with curl​

export BAAS_URL="https://<ref>.baas.arianet.app"
export ANON_KEY="<ANON_KEY>"

curl "$BAAS_URL/rest/v1/todos?select=*" \
-H "apikey: $ANON_KEY"

The answer is []: a request without a signed-in User runs as the anon role, and the "Each User only their own Rows" Policy does not let it see anything. RLS is doing its job.

Now create a User, sign in and write a Row with their token:

# Sign up (if email confirmation is on, confirm the email first, or create the User in the panel as confirmed)
curl -X POST "$BAAS_URL/auth/v1/signup" \
-H "apikey: $ANON_KEY" -H "Content-Type: application/json" \
-d '{"email": "ali@example.com", "password": "a-strong-password"}'

# Sign in with the password: take access_token from the answer
curl -X POST "$BAAS_URL/auth/v1/token?grant_type=password" \
-H "apikey: $ANON_KEY" -H "Content-Type: application/json" \
-d '{"email": "ali@example.com", "password": "a-strong-password"}'

export TOKEN="<access_token>"

# Insert a Row; user_id is filled with this User's id automatically
curl -X POST "$BAAS_URL/rest/v1/todos" \
-H "apikey: $ANON_KEY" -H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" -H "Prefer: return=representation" \
-d '{"title": "Buy bread"}'

# Read: only this User's Rows
curl "$BAAS_URL/rest/v1/todos?select=id,title,created_at&order=created_at.desc" \
-H "apikey: $ANON_KEY" -H "Authorization: Bearer $TOKEN"

5. First request with JavaScript​

Any client library compatible with Postgres/BaaS works. The exact client package name is shown in the Connection code samples («نمونه‌کد اتصال») card under API Keys and Connect; put it in place of <client-package> below:

import { createClient } from '<client-package>'

const client = createClient('https://<ref>.baas.arianet.app', '<ANON_KEY>')

// Sign in
const { data: session, error: loginError } = await client.auth.signInWithPassword({
email: 'ali@example.com',
password: 'a-strong-password',
})

// Read Rows (RLS returns only this User's Rows)
const { data, error } = await client.from('todos').select('*')

Without a library, fetch works too:

const BAAS_URL = 'https://<ref>.baas.arianet.app'
const ANON_KEY = '<ANON_KEY>'

const res = await fetch(`${BAAS_URL}/rest/v1/todos?select=*`, {
headers: { apikey: ANON_KEY },
})
const todos = await res.json()

Next steps​

  • REST API: filtering, ordering, paging, upsert and calling Functions.
  • Auth API: one-time codes, password recovery and auth.uid() in Policies.
  • Storage: uploading files and Signed URLs.
  • CLI: arianet baas env prints your app's environment variables.