Home

Generating Types

How to generate types for your API and Supabase libraries.

Supabase APIs are generated from your database, which means that we can use database introspection to generate type-safe API definitions.

Generating types using Supabase CLI#

The Supabase CLI is a single binary Go application that provides everything you need to setup a local development environment.

You can install the CLI via npm or other supported package managers. The minimum required version of the CLI is v1.8.1.


_10
npm i supabase@">=1.8.1" --save-dev

Login with your Personal Access Token:


_10
npx supabase login

Generate types for your project to produce the types/supabase.ts file:


_10
npx supabase gen types typescript --project-id "$PROJECT_REF" --schema public > types/supabase.ts

After you have generated your types, you can use them in src/index.ts


_13
import { NextApiRequest, NextApiResponse } from 'next'
_13
import { createClient } from '@supabase/supabase-js'
_13
import { Database } from '../types/supabase'
_13
_13
const supabase = createClient<Database>(
_13
process.env.NEXT_PUBLIC_SUPABASE_URL,
_13
process.env.SUPABASE_SECRET_KEY
_13
)
_13
_13
export default async (req: NextApiRequest, res: NextApiResponse) => {
_13
const allOnlineUsers = await supabase.from('users').select('*').eq('status', 'ONLINE')
_13
res.status(200).json(allOnlineUsers)
_13
}

Update types automatically with GitHub Actions#

One way to keep your type definitions in sync with your database is to set up a GitHub action that runs on a schedule.

Add the script above to your package.json to run it using npm run update-types


_10
"update-types": "npx supabase gen types typescript --project-id \"$PROJECT_REF\" > types/supabase.ts"

Create a file .github/workflows/update-types.yml with the following snippet to define the action along with the environment variables. This script will commit new type changes to your repo every night.


_39
name: Update database types
_39
_39
on:
_39
schedule:
_39
# sets the action to run daily. You can modify this to run the action more or less frequently
_39
- cron: '0 0 * * *'
_39
_39
jobs:
_39
update:
_39
runs-on: ubuntu-latest
_39
env:
_39
SUPABASE_ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}
_39
PROJECT_REF: <your-project-id>
_39
steps:
_39
- uses: actions/checkout@v2
_39
with:
_39
persist-credentials: false
_39
fetch-depth: 0
_39
- uses: actions/setup-node@v2.1.5
_39
with:
_39
node-version: 16
_39
- run: npm run update-types
_39
- name: check for file changes
_39
id: git_status
_39
run: |
_39
echo "status=$(git status -s)" >> $GITHUB_OUTPUT
_39
- name: Commit files
_39
if: ${{contains(steps.git_status.outputs.status, ' ')}}
_39
run: |
_39
git add types/database/index.ts
_39
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
_39
git config --local user.name "github-actions[bot]"
_39
git commit -m "Update database types" -a
_39
- name: Push changes
_39
if: ${{contains(steps.git_status.outputs.status, ' ')}}
_39
uses: ad-m/github-push-action@master
_39
with:
_39
github_token: ${{ secrets.GITHUB_TOKEN }}
_39
branch: ${{ github.ref }}

Alternatively, you can use a community-supported GitHub action: generate-supabase-db-types-github-action.

Resources#