useGrant

Neon Data API

Query Neon's Data API with access tokens from useGrant.

Neon's Data API accepts JSON Web Tokens from custom authentication providers. You can configure useGrant as that provider and query your database over HTTP with the access tokens it issues.

Prerequisites

  • A useGrant provider with a client. Set the client's audience to the value you will configure in Neon, for example api.neon.tech.
  • A Neon project

Step 1: Enable the Data API

  1. In the Neon console, select your project, then Postgres database → Data API.
  2. Click Enable Data API.

Step 2: Add useGrant as an authentication provider

  1. On the Data API page, open the Settings tab.
  2. Select Other Provider.
  3. Enter the JWKS URL of your useGrant provider, for example https://auth.example.com/acme-prod/.well-known/jwks. If you set up a custom domain, use https://<CUSTOM_DOMAIN>/.well-known/jwks instead.
  4. Enter the audience of your useGrant client as the JWT Audience.
  5. Save the settings.

See the Neon documentation on custom authentication providers for more details.

Step 3: Secure your tables

The Data API has no permission layer of its own, so access is controlled with GRANT statements and row-level security. Enable row-level security on every table you expose, and write policies against the token's sub claim with auth.user_id(). For a useGrant token, sub is the client's subject.

ALTER TABLE todos ENABLE ROW LEVEL SECURITY;

CREATE POLICY "clients read their own todos" ON todos
  FOR SELECT USING (owner = auth.user_id());

Caution

Tables without row-level security are readable by any request with a valid token.

Step 4: Query the Data API

Create an access token for your client and send it as a Bearer token. Copy your Data API URL from the Data API page in the Neon console.

import UseGrant from "@usegrant/sdk";

const usegrant = new UseGrant(process.env.USEGRANT_API_KEY);

const { accessToken } = await usegrant.createToken("providerId", "clientId");

const response = await fetch(`${process.env.NEON_DATA_API_URL}/todos`, {
  headers: { Authorization: `Bearer ${accessToken}` },
});

const todos = await response.json();

On this page