Branching with the Neon API
Learn how to create and delete branches with the Neon API
The examples in this guide demonstrate creating, viewing, and deleting branches using the Neon API. For other branch-related API methods, refer to the Neon API reference.
note
The API examples that follow may only show some of the user-configurable request body attributes that are available to you. To view all attributes for a particular method, refer to the method's request body schema in the Neon API reference.
The jq
program specified in each example is an optional third-party tool that formats the JSON
response, making it easier to read. For information about this utility, see jq.
Prerequisites
A Neon API request requires an API key. For information about obtaining an API key, see Create an API key. In the examples below, $NEON_API_KEY
is specified in place of an actual API key, which you must provide when making a Neon API request.
Create a branch with the API
The following Neon API method creates a branch. To view the API documentation for this method, refer to the Neon API reference.
The API method appears as follows when specified in a cURL command:
note
This method does not require a request body. Without a request body, the method creates a branch from the project's default branch, and a compute is not created.
- The
project_id
for a Neon project is found on the Settings page in the Neon Console, or you can find it by listing the projects for your Neon account using the Neon API. It is a generated value that looks something like this:autumn-disk-484331
. - The
endpoints
attribute creates a compute, which is required to connect to the branch. Neon supportsread_write
andread_only
compute types. A branch can be created with or without a compute. You can specifyread_only
to create a read replica. - The
branch
attribute specifies the parent branch. - The
parent_id
can be obtained by listing the branches for your project. See List branches. Theparent_id
is theid
of the branch you are branching from. A branchid
has abr-
prefix. You can branch from your Neon project's default branch or a non-default branch.
The response includes information about the branch, the branch's compute, and the create_branch
and start_compute
operations that were initiated.
List branches with the API
The following Neon API method lists branches for the specified project. To view the API documentation for this method, refer to the Neon API reference.
The API method appears as follows when specified in a cURL command:
The project_id
for a Neon project is found on the Settings page in the Neon Console, or you can find it by listing the projects for your Neon account using the Neon API.
The response lists the project's default branch and any child branches. The name of the default branch in this example is main
.
Response:
Delete a branch with the API
The following Neon API method deletes the specified branch. To view the API documentation for this method, refer to the Neon API reference.
The API method appears as follows when specified in a cURL command:
- The
project_id
for a Neon project is found on the Settings page in the Neon Console, or you can find it by listing the projects for your Neon account using the Neon API. - The
branch_id
can be found by listing the branches for your project. The<branch_id>
is theid
of a branch. A branchid
has abr-
prefix. See List branches.
The response shows information about the branch being deleted and the suspend_compute
and delete_timeline
operations that were initiated.
You can verify that a branch is deleted by listing the branches for your project. See List branches. The deleted branch should no longer be listed.
Restoring a branch using the API
To revert changes or recover lost data, you can use the branch restore endpoint in the Neon API.
For details on how to use this endpoint to restore a branch to its own or another branch's history, restore a branch to the head of its parent, and other restore options, see Branch Restore using the API.
Need help?
Join our Discord Server to ask questions or see what others are doing with Neon. Users on paid plans can open a support ticket from the console. For more details, see Getting Support.