# Add a backend to a Swift app

> Give an iOS or macOS app a Postgres database, auth, storage and realtime with supabase-swift, and generate Codable types for your tables with one command.

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

A Swift app uses the supabase-swift package, which works with Scribase unchanged. Create the project in the console, copy two values, and generate Codable types for your tables.

## Environment file: Config.xcconfig or the app Info.plist

- 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 package

In Xcode: File, Add Package Dependencies, then this URL. Or add it to Package.swift.

_Package.swift_
```swift
.package(url: "https://github.com/supabase/supabase-swift.git", from: "2.55.0")
```

## Create the client

The URL and the publishable key are public by design; row-level security protects the data.

```swift
import Supabase

let supabase = SupabaseClient(
  supabaseURL: URL(string: "https://acme-my-app-production-c2b0ea0a.scribase.com")!,
  supabaseKey: "<publishable key>"
)
```

## 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 Codable types

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

```bash
scribase gen types <org> <project> production --lang swift --out Sources/App/DatabaseTypes.swift
```

## Query with the types

Rows decode into the generated structs.

```swift
let todos: [TodosRow] = try await supabase.from("todos").select().execute().value
```

## Agent prompt

```
Add a Scribase backend to this Swift app.
1. Add the supabase-swift package (https://github.com/supabase/supabase-swift.git, from 2.55.0).
2. Create a SupabaseClient from the project URL and publishable key; read both from the app configuration, never hard-code 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 swift` into the app and list the todos in a SwiftUI view.
```

## FAQ

**Do sign-in with Apple and passkeys work?**

Yes. Turn them on for the environment in the console; supabase-swift calls the same auth endpoints.