N
Neon15mo ago
eastern-cyan

restore branch for node.js

I want to use the node.js library to execute a restore branch action (https://api-docs.neon.tech/reference/restoreprojectbranch) the documentation seems to be out of date... Can I get an example of how I can make this request? Specifically, I want to restore a staging branch with the latest data from main.
Neon
Restore a branch
Restores a branch to an earlier state in its own or another branch's history
5 Replies
flat-fuchsia
flat-fuchsia15mo ago
Sure thing! Here's a Next.js endpoint
export const dynamic = 'force-dynamic'

export const fetchCache = 'force-no-store'

import { NextRequest, NextResponse } from 'next/server'

export async function GET(request: NextRequest) {
const searchParams = request.nextUrl.searchParams
const branchName = searchParams.get('branchName')
const headers = new Headers()
headers.append('Accept', 'application/json')
headers.append('Content-Type', 'application/json')
headers.append('Authorization', `Bearer ${process.env.NEON_API_KEY}`)
const body = JSON.stringify({ source_branch_id: process.env.NEON_PARENT_ID })
const start_time = performance.now()
await fetch(`https://console.neon.tech/api/v2/projects/${process.env.NEON_PROJECT_ID}/branches/${branchName}/restore`, {
method: 'POST',
headers,
body,
})
const end_time = performance.now()
return NextResponse.json({
time: end_time - start_time,
code: 1,
})
}
export const dynamic = 'force-dynamic'

export const fetchCache = 'force-no-store'

import { NextRequest, NextResponse } from 'next/server'

export async function GET(request: NextRequest) {
const searchParams = request.nextUrl.searchParams
const branchName = searchParams.get('branchName')
const headers = new Headers()
headers.append('Accept', 'application/json')
headers.append('Content-Type', 'application/json')
headers.append('Authorization', `Bearer ${process.env.NEON_API_KEY}`)
const body = JSON.stringify({ source_branch_id: process.env.NEON_PARENT_ID })
const start_time = performance.now()
await fetch(`https://console.neon.tech/api/v2/projects/${process.env.NEON_PROJECT_ID}/branches/${branchName}/restore`, {
method: 'POST',
headers,
body,
})
const end_time = performance.now()
return NextResponse.json({
time: end_time - start_time,
code: 1,
})
}
eastern-cyan
eastern-cyanOP15mo ago
thanks @Rishi Raj Jain ! I'm looking for something similar to this. How can I pass in the branch I want to restore FROM?
const response = await neonClient.restoreProjectBranch({
project_id: projectId,
branch_id: branchToRestoreId,
data: {

}
});
const response = await neonClient.restoreProjectBranch({
project_id: projectId,
branch_id: branchToRestoreId,
data: {

}
});
flat-fuchsia
flat-fuchsia15mo ago
.restoreProjectBranch({source_branch_id: 'a'}, {project_id: 'b', branch_id: 'c'}) you can specify the source_branch_id
eastern-cyan
eastern-cyanOP15mo ago
can you please confirm? Seeing this error @Rishi Raj Jain
No description
flat-fuchsia
flat-fuchsia15mo ago
Yeah, per https://api-docs.neon.tech/reference/restoreprojectbranch this should be possible Can you share which SDK are you using?

Did you find this page helpful?