Skip to main content

Generating Types

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

Generating types from OpenAPI specification

Supabase generates an OpenAPI specification file for your database which can be used to generate your data types for usage with TypeScript.

The OpenAPI specification for your Supabase project can be accessed as follows:

Using the open source openapi-typescript tool you can generate your types and store them locally:

npx openapi-typescript --output types/supabase.ts

Important notes:

  • Since the generator uses JSON API, there is no way to determine if a column is an Array. It will generate array types as string, even though Supabase handles this automatically and returns arrays. You can fix this manually in the files by changing the type, e.g. names: string -> names: string[]
  • The types won't automatically stay in sync with your database, so make sure to regenerate your types after your make changes to your database.

After you have generated your types, you can use them in your TypeScript projects:

import { NextApiRequest, NextApiResponse } from 'next'
import { createClient } from '@supabase/supabase-js'
import { definitions } from '../../types/supabase'

const supabase = createClient(

export default async (req: NextApiRequest, res: NextApiResponse) => {
const allOnlineUsers = await supabase
.eq('status', 'ONLINE')

For more advance type-support, check out postgrest-js-tools.

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.

The following script can be run in your terminal to produce the file types/database/index.ts.

npx openapi-typescript --output types/database/index.ts

You can add this script to your package.json and run it using npm run update-types:

"update-types": "npx openapi-typescript \"${SUPABASE_URL}/rest/v1/?apikey=${SUPABASE_ANON_KEY}\" --version=2 --output types/database/index.ts"

You can use GitHub actions to generate this file automatically. This script will commit the change to your repo every night. Create a file .github/workflows/update-types.yml and add the following snippet into this file to define the action along with the environment variables.

name: Update database types

# sets the action to run daily. You can modify this to run the action more or less frequently
- cron: '0 0 * * *'

runs-on: ubuntu-latest
- uses: actions/checkout@v2
persist-credentials: false
fetch-depth: 0
- uses: actions/setup-node@v2.1.5
node-version: 14
- run: npm run update-types
- name: check for file changes
id: git_status
run: |
echo "::set-output name=status::$(git status -s)"
- name: Commit files
if: ${{contains(steps.git_status.outputs.status, ' ')}}
run: |
git add types/database/index.ts
git config --local "41898282+github-actions[bot]"
git config --local "github-actions[bot]"
git commit -m "Update database types" -a
- name: Push changes
if: ${{contains(steps.git_status.outputs.status, ' ')}}
uses: ad-m/github-push-action@master
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: ${{ github.ref }}

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