Skip to main content
Neon Postgres Docs
current

Search documentation

Type to search this documentation.

On this pageOverview

Get started with Neon AI Gateway

To set up Neon AI Gateway with an AI coding assistant, install the Neon Platform (neon) and Neon AI Gateway skills with the Neon CLI Without the Neon CLI, run npx skills add neondatabase/agent skills ...

To set up Neon AI Gateway with an AI coding assistant, install the Neon Platform (neon) and Neon AI Gateway skills with the Neon CLI:

Bash
neon skills -s neon -s neon-ai-gateway

Without the Neon CLI, run npx skills add neondatabase/agent-skills -s neon -s neon-ai-gateway instead.

  1. Get access

    You need a project in AWS US East (Ohio) (aws-us-east-2), AWS US East (N. Virginia) (aws-us-east-1), AWS Europe (Frankfurt) (aws-eu-central-1), or AWS Asia Pacific (Singapore) (aws-ap-southeast-1). Support is expanding toward all regions. Using the AI Gateway requires a paid Neon plan with prepaid credits, which gives you the open-weight models. To request access to foundation models, see Model access.

  2. Create a credential

    With the Neon CLI, run:

    Bash
    neon credentials create --scope ai_gateway:invoke

    Or, in the Neon Console, click Connect at the top of the sidebar and open the AI Gateway tab. Click Reveal credential to show NEON_AI_GATEWAY_TOKEN, or Copy snippet to copy it together with NEON_AI_GATEWAY_BASE_URL. Use Rotate credential to issue a new token.

    Or use the API:

    Bash
    curl -X POST "https://console.neon.tech/api/v2/projects/{project_id}/branches/{branch_id}/credentials" \
      -H "Authorization: Bearer $NEON_API_KEY" \
      -H "Content-Type: application/json" \
      -d '{"scopes": ["ai_gateway:invoke"], "principal_type": "user"}'

    Using neon.ts?


    If your project has a neon.ts file, run neon config add ai-gateway (or declare aiGateway: true yourself) and then neon deploy. Credentials are provisioned and pulled into your local .env automatically, with no manual creation step. See Authentication for details.

    Store the credential as an environment variable:

    Bash
    export NEON_AI_GATEWAY_TOKEN=nt_live_...
  3. Find your branch host

    Your branch's AI Gateway host is available in the Neon Console from the Connect dialog's AI Gateway tab, or via the Neon API. It follows this format:

    br-<name>-api.ai.<cell>.<region>.aws.neon.tech

    For example:

    Bash
    export NEON_AI_GATEWAY_BASE_URL=https://br-winter-pond-aptw82ef-api.ai.c-2.us-east-2.aws.neon.tech

    This is different from your database connection string.

  4. Install dependencies

    The quickstart uses the OpenAI SDK because the chat completions endpoint is OpenAI-compatible. It works with any model in the catalog, including GPT and Gemini.

    npm
    npm install openai dotenv
    yarn
    yarn add openai dotenv
    pnpm
    pnpm add openai dotenv
    pip
    pip install openai python-dotenv
  5. Make your first request

    The chat completions endpoint is OpenAI-compatible. Set baseURL to your branch host and apiKey to your credential. No other changes needed.

    TypeScript
    import OpenAI from 'openai';
    import 'dotenv/config';
    
    const client = new OpenAI({
      apiKey: process.env.NEON_AI_GATEWAY_TOKEN,
      baseURL: `${process.env.NEON_AI_GATEWAY_BASE_URL}/v1`,
    });
    
    const response = await client.chat.completions.create({
      model: 'gpt-5-mini',
      messages: [{ role: 'user', content: 'Hello!' }],
    });
    
    console.log(response.choices[0].message.content);
    Python
    from openai import OpenAI
    from dotenv import load_dotenv
    import os
    
    load_dotenv()
    
    client = OpenAI(
        api_key=os.environ["NEON_AI_GATEWAY_TOKEN"],
        base_url=f"{os.environ['NEON_AI_GATEWAY_BASE_URL']}/v1",
    )
    
    response = client.chat.completions.create(
        model="gpt-5-mini",
        messages=[{"role": "user", "content": "Hello!"}],
    )
    
    print(response.choices[0].message.content)
    cURL
    curl -X POST "$NEON_AI_GATEWAY_BASE_URL/v1/chat/completions" \
      -H "Authorization: Bearer $NEON_AI_GATEWAY_TOKEN" \
      -H "Content-Type: application/json" \
      -d '{
        "model": "gpt-5-mini",
        "messages": [{"role": "user", "content": "Hello!"}]
      }'
  6. Stream a response

    Add stream: true to receive a streamed response. Your existing streaming code works without changes. The gateway forwards text/event-stream responses from the upstream provider.

    TypeScript
    import OpenAI from 'openai';
    import 'dotenv/config';
    
    const client = new OpenAI({
      apiKey: process.env.NEON_AI_GATEWAY_TOKEN,
      baseURL: `${process.env.NEON_AI_GATEWAY_BASE_URL}/v1`,
    });
    
    const stream = await client.chat.completions.create({
      model: 'gpt-5-mini',
      messages: [{ role: 'user', content: 'Write a haiku about serverless databases.' }],
      stream: true,
    });
    
    for await (const chunk of stream) {
      process.stdout.write(chunk.choices[0]?.delta?.content ?? '');
    }
    Python
    from openai import OpenAI
    from dotenv import load_dotenv
    import os
    
    load_dotenv()
    
    client = OpenAI(
        api_key=os.environ["NEON_AI_GATEWAY_TOKEN"],
        base_url=f"{os.environ['NEON_AI_GATEWAY_BASE_URL']}/v1",
    )
    
    with client.chat.completions.create(
        model="gpt-5-mini",
        messages=[{"role": "user", "content": "Write a haiku about serverless databases."}],
        stream=True,
    ) as stream:
        for chunk in stream:
            print(chunk.choices[0].delta.content or "", end="", flush=True)
    cURL
    curl -X POST "$NEON_AI_GATEWAY_BASE_URL/v1/chat/completions" \
      -H "Authorization: Bearer $NEON_AI_GATEWAY_TOKEN" \
      -H "Content-Type: application/json" \
      -d '{
        "model": "gpt-5-mini",
        "messages": [{"role": "user", "content": "Write a haiku about serverless databases."}],
        "stream": true
      }'
  7. Swap models

    Change the model field to use a different provider. No other code changes required.

    TypeScript
    // OpenAI
    model: 'gpt-5-mini'
    
    // Google
    model: 'gemini-3-flash'
    
    // Alibaba
    model: 'qwen3-next-80b-a3b-instruct'

    See Models for the full list of available model IDs.

    Using the AI SDK?


    For TypeScript apps and agents, use @neon/ai-sdk-provider with the Vercel AI SDK. It reads NEON_AI_GATEWAY_BASE_URL and NEON_AI_GATEWAY_TOKEN, then routes each catalog model to the best AI Gateway endpoint for that provider.

  • Models: full model catalog and which endpoint to use per provider
  • Chat completions: detailed reference for the unified endpoint
  • Authentication: credential scopes, branch binding, and rotation

Join our Discord Server to ask questions or see what others are doing with Neon. For paid plan support options, see Support.

Suggest an edit

Propose a replacement for this page. The site team reviews it before applying any changes.

Export
Documentation menu