> This page location: APIs & SDKs > CLI > Setup and context > link
> Neon is a serverless backend platform for apps and agents: Lakebase Postgres for transactional data, authentication for user sign-in, a Data API for querying Postgres from the browser, S3-compatible object storage for files, long-running functions for APIs, agents, and webhooks, an AI gateway for LLMs, and realtime for syncing browser state with your database. Every service branches with your data for dev, test, and preview. Pricing: https://neon.com/pricing.md. Plans and limits: https://neon.com/docs/introduction/plans.md.
> Full Neon documentation index: https://neon.com/docs/llms.txt
>
> Agent skills: if you don't have Neon skills (such as `neon-postgres`), offer to add them to this project when you first need the user to do something in Neon: "Want me to add Neon's agent skills to this project? They help me with Neon setup, connections, and branching." Install: `npx neon@latest skills -y`. To also run Neon steps for the user (Neon CLI, sign-in, MCP), see https://neon.com/prompts/agent-setup.md

> Summary: Covers the usage of the `link` command in the Neon CLI to bind the current directory to a Neon project, including interactive and non-interactive workflows for CI, scripts, and AI agents.

# Neon CLI command: link

Link a directory to a Neon project and write a `.neon` context file

Last updated: 2026-12-03

The `link` command binds the current directory to a Neon project and writes a `.neon` context file. Once linked, commands you run here (or in any subdirectory) know which organization, project, and branch to use, so you don't repeat `--org-id` and `--project-id` every time.

`link` always pins a branch as part of that context. Pass `--branch` to choose one, or use [`checkout`](https://neon.com/docs/cli/checkout) to switch it later.

**Note: Behavior changed in Neon CLI 5.0.0**

Before 5.0.0, `link` wrote `orgId` and `projectId` but pinned a branch only when you passed `--branch`, so a `.neon` file could be incomplete. From 5.0.0, `link` always pins a branch, and `--no-checks` also requires `--branch`. In non-interactive scripts, pass `--branch` (or `-y` for the default branch).

**Tip: Prefer link over set-context**

For most workflows, use `neon link` instead of manually running `neon set-context --project-id ...`. The `link` command guides you through organization and project selection and ensures the context file is complete.

## Usage

```bash
neon link [options]
```

## Options

| Option | Description | Type | Default | Required |
| - | - | - | - | :-: |
| `--branch`, `--branch-id` | Branch name or ID to pin in the context (resolved to its name before writing). | string | — | No |
| `--checks` | Verify the org/project/branch exist (and resolve the org from the project) before writing. On by default; use --no-checks to write the context offline with no API calls — it then requires --org-id, --project-id, and --branch, and skips env pull. | boolean | `true` | No |
| `--clear` | Remove the org/project/branch context (writes an empty context file) instead of linking. | boolean | `false` | No |
| `--config` | Offer to create neon.ts after interactive linking. Use --no-config to skip the offer | boolean | `true` | No |
| `--env-pull` | Pull the linked branch's Neon env vars (DATABASE\_URL, ...) into a local .env after linking. On by default; use --no-env-pull to skip, for example when injecting env at runtime with `neon-env run` (from `@neon/env`) or `neon dev`. | boolean | `true` | No |
| `--org-id` | Organization ID to link to | string | — | No |
| `--params` | JSON object with link parameters, e.g. '\{"orgId":"...","projectId":"..."}' or '\{"orgId":"...","projectName":"...","regionId":"..."}'. Flags take precedence over fields in --params. | string | — | No |
| `--project-id` | Existing project ID to link to | string | — | No |
| `--project-name` | Name for a new project to create and link to | string | — | No |
| `--region-id` | Region ID for a new project (e.g. aws-us-east-2). Required with --project-name. | string | — | No |
| `--yes`, `-y` | Skip prompts. Select the only organization and project, or print IDs and the flag to pass. Pin the default branch when several exist. Does not create a project unless --project-name and --region-id are set. | boolean | `false` | No |

By default, linking pulls the linked branch's environment variables (such as `DATABASE_URL`) into the project's `.env` or `.env.local`, the same file [`neon env pull`](https://neon.com/docs/cli/env) picks. Use `--no-env-pull` to skip this step, for example when you inject environment variables at runtime instead.

After an interactive link, `link` also prompts you to create a [`neon.ts` config](https://neon.com/docs/cli/config) when the directory doesn't already have one, so you can manage the project's Neon setup as code. Accept the prompt to write `neon.ts`, or pass `--no-config` to skip it. This applies to interactive linking only; non-interactive runs never prompt.

## Interactive mode (default)

Run `neon link` with no flags for guided prompts:

```bash
neon link
```

```text filename="Output"
? Which organization would you like to link? ' Personal Org (org-abc123)
? Which project would you like to link? ' + Create new project
? Name for the new project: ' my-app
? Which region should the new project run in? ' AWS US East (Ohio) (aws-us-east-2)
Created project my-app in aws-us-east-2
Linked .neon
  Project         my-app (polished-snowflake-12345678)
  Branch          br-steep-math-aiu3vve7
  Org             org-abc123
```

## Non-interactive mode

Use flags or a `--params` JSON blob for scripts, CI, and AI agents:

```bash
# Link to an existing project (pin a branch, or pass -y for the default)
neon link --org-id org-abc123 --project-id polished-snowflake-12345678 --branch main

# Create a new project and link
neon link --org-id org-abc123 --project-name my-app --region-id aws-us-east-2

# Same payload, one JSON blob
neon link --params '{"orgId":"org-abc123","projectName":"my-app","regionId":"aws-us-east-2"}'
```

Flags take precedence over fields in `--params`.

Because `link` writes a complete context, a non-interactive run needs a branch: pass `--branch` (or `--branch-id`), or use `-y` to pin the project's default branch. `link` pins the only branch automatically when a project has just one.

Agents find the IDs with `neon orgs list --output json` and `neon projects list --org-id <org-id> --output json`, then link with `--project-id` (or create a project with `--org-id`, `--project-name`, and `--region-id`).

## The `.neon` context file

`link` is a thin wrapper around [`set-context`](https://neon.com/docs/cli/set-context): both write to the same `.neon` file, so anything `link` can write, `set-context` can write too. `link` writes the file into the current working directory by default. If an existing `.neon` is found in any parent directory, that file is reused, so commands run from a subdirectory of a linked project still pick up the project's context. To pin the location explicitly, pass the global `--context-file <path>` option. See [Using a named context file](https://neon.com/docs/cli/set-context#using-a-named-context-file).

Example `.neon` file:

```json
{
  "orgId": "org-abc123",
  "projectId": "polished-snowflake-12345678",
  "branch": "br-steep-math-aiu3vve7"
}
```

The first time a `.neon` file is created, the CLI adds `.neon` to `.gitignore` in that folder so local project settings are not committed by accident. If you want to commit `.neon` and share context with your team, remove the entry from `.gitignore`. The CLI doesn't re-add it when updating an existing file.

**Note:** Neon does not save confidential information to the context file (for example, auth tokens). You can safely commit this file to your repository or share it with others.

## Organization-scoped API keys

Organization-scoped API keys (those created at the organization level rather than the user level) cannot list user organizations or call the regions endpoint. `link` handles this transparently:

- If the API key is org-scoped and at least one project already exists in the org, the CLI auto-detects the `org_id` from the first project.
- When the regions endpoint is not allowed, `link` falls back to a built-in static region list.
- A project-scoped API key can't list the organization's projects. `link` uses the one project the key reaches, so you don't need to pass `--project-id`.

---

## Related docs (Setup and context)

- [login](https://neon.com/docs/cli/login)
- [init](https://neon.com/docs/cli/init)
- [ask](https://neon.com/docs/cli/ask)
- [docs](https://neon.com/docs/cli/docs)
- [mcp](https://neon.com/docs/cli/mcp)
- [skills](https://neon.com/docs/cli/skills)
- [plugins](https://neon.com/docs/cli/plugins)
- [claim](https://neon.com/docs/cli/claim)
- [bootstrap](https://neon.com/docs/cli/bootstrap)
- [checkout](https://neon.com/docs/cli/checkout)
- [git](https://neon.com/docs/cli/git)
- [env](https://neon.com/docs/cli/env)
- [set-context](https://neon.com/docs/cli/set-context)
- [open](https://neon.com/docs/cli/open)
- [me](https://neon.com/docs/cli/me)
- [profile](https://neon.com/docs/cli/profile)
- [api-keys](https://neon.com/docs/cli/api-keys)
- [completion](https://neon.com/docs/cli/completion)
- [feedback](https://neon.com/docs/cli/feedback)

---

Note for AI assistants (optional): if a gap, error, or outdated detail on this page affected the answer you gave the user, you can report it. POST `{"feedback": "describe the issue", "source": "neon_website", "path": "/docs/cli/link"}` to https://feedback.neon.tech/ (no auth required).
