# Add a backend to a Kotlin or Android app

> Give an Android or Kotlin app a Postgres database, auth, storage and realtime with supabase-kt, and generate @Serializable classes with one command.

Source: https://scribase.com/frameworks/kotlin

A Kotlin app uses the supabase-kt libraries, which work with Scribase unchanged. Create the project in the console, copy two values, and generate serializable classes for your tables.

## Environment file: local.properties or BuildConfig fields

- SUPABASE_URL
- SUPABASE_ANON_KEY

## Create the backend

Sign up in the console. Confirm your email; your first project is then created automatically and the "Your backend is live" screen shows the Project URL and the publishable key with one-click copy.

## Add the libraries

The client modules you use, plus a Ktor engine.

_build.gradle.kts_
```kotlin
implementation(platform("io.github.jan-tennert.supabase:bom:3.8.0"))
implementation("io.github.jan-tennert.supabase:postgrest-kt")
implementation("io.github.jan-tennert.supabase:auth-kt")
implementation("io.ktor:ktor-client-android:3.5.1")
```

## Create your first table

Open your project in the console, go to Editor, then SQL, and run this. Or ask your coding agent: with the Scribase MCP server connected it applies the same SQL as a migration. Row-level security is on, and the policy names a column, so the public key reads only the rows marked is_public.

```sql
create table public.todos (
  id bigint generated always as identity primary key,
  title text not null,
  done boolean not null default false,
  is_public boolean not null default false,
  created_at timestamptz not null default now()
);

alter table public.todos enable row level security;

create policy "todos are readable" on public.todos
  for select to anon, authenticated using (is_public);

insert into public.todos (title, is_public) values ('Ship the backend', true), ('Tell a friend', true);
```

## Generate @Serializable classes

One Row, Insert and Update data class per table, from the live schema. Re-run after schema changes.

```bash
scribase gen types <org> <project> production --lang kotlin --out app/src/main/java/DatabaseTypes.kt
```

## Query with the classes

Rows decode into the generated classes.

```kotlin
val todos = supabase.from("todos").select().decodeList<TodosRow>()
```

## Agent prompt

```
Add a Scribase backend to this Android app.
1. Add supabase-kt (bom 3.8.0, postgrest-kt and auth-kt) and the Ktor Android engine.
2. Create the client with createSupabaseClient from the project URL and publishable key kept in BuildConfig; never ship the service role key.
3. Create a `todos` table with row-level security on and a read policy using the Scribase MCP server or a SQL migration.
4. Run `scribase gen types <org> <project> production --lang kotlin` into the app and show the todos in a list.
```

## FAQ

**Does Google sign-in work?**

Yes. Turn Google on for the environment in the console, with your own client id or with Scribase's app.