Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 10
feat: add POST /api/sandbox endpoint#155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
3d564154d894c4a314b247ef36d5c96bac3File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { NextRequest, NextResponse } from "next/server"; | ||
| import { getCorsHeaders } from "@/lib/networking/getCorsHeaders"; | ||
| import { createSandboxPostHandler } from "@/lib/sandbox/createSandboxPostHandler"; | ||
| /** | ||
| * OPTIONS handler for CORS preflight requests. | ||
| * | ||
| * @returns A NextResponse with CORS headers. | ||
| */ | ||
| export async function OPTIONS() { | ||
| return new NextResponse(null, { | ||
| status: 200, | ||
| headers: getCorsHeaders(), | ||
| }); | ||
| } | ||
| /** | ||
| * POST /api/sandbox | ||
| * | ||
| * Creates a new ephemeral sandbox environment. | ||
| * Sandboxes are isolated Linux microVMs that can be used to evaluate | ||
| * account-generated code, run AI agent output safely, or execute reproducible tasks. | ||
| * | ||
| * Request: | ||
| * - No request body required | ||
| * - Authentication via x-api-key header | ||
| * | ||
| * Response: | ||
| * - 200: { sandboxId: string, status: string, timeout: number, createdAt: string } | ||
| * - 400: { error: "Failed to create sandbox" } | ||
| * - 401: { status: "error", error: "x-api-key header required" or "Invalid API key" } | ||
| * | ||
| * @param request - The request object | ||
| * @returns A NextResponse with the created sandbox data or error | ||
| */ | ||
| export async function POST(request: NextRequest) { | ||
| return createSandboxPostHandler(request); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { Sandbox } from "@vercel/sandbox"; | ||
| /** | ||
| * Response from creating a sandbox. | ||
| * Uses Sandbox class types from @vercel/sandbox SDK. | ||
| */ | ||
| export interface SandboxCreatedResponse { | ||
| sandboxId: Sandbox["sandboxId"]; | ||
| status: Sandbox["status"]; | ||
| timeout: Sandbox["timeout"]; | ||
| createdAt: string; | ||
| } | ||
| /** | ||
| * Creates a new ephemeral sandbox environment using Vercel Sandbox SDK. | ||
| * | ||
| * @returns The created sandbox details | ||
| * @throws Error if sandbox creation fails | ||
| */ | ||
| export async function createSandbox(): Promise<SandboxCreatedResponse> { | ||
| const sandbox = await Sandbox.create(); | ||
| return { | ||
| sandboxId: sandbox.sandboxId, | ||
| status: sandbox.status, | ||
| timeout: sandbox.timeout, | ||
| createdAt: sandbox.createdAt.toISOString(), | ||
| }; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { NextRequest, NextResponse } from "next/server"; | ||
| import { getCorsHeaders } from "@/lib/networking/getCorsHeaders"; | ||
| import { getApiKeyAccountId } from "@/lib/auth/getApiKeyAccountId"; | ||
| import { createSandbox } from "@/lib/sandbox/createSandbox"; | ||
| /** | ||
| * Handler for POST /api/sandbox. | ||
| * | ||
| * Creates a new ephemeral sandbox environment. Requires authentication via x-api-key header. | ||
| * No request body is required. | ||
| * | ||
| * @param request - The request object | ||
| * @returns A NextResponse with sandbox data or error | ||
| */ | ||
| export async function createSandboxPostHandler(request: NextRequest): Promise<NextResponse> { | ||
| const accountIdOrError = await getApiKeyAccountId(request); | ||
| if (accountIdOrError instanceof NextResponse) { | ||
| return accountIdOrError; | ||
| } | ||
| try { | ||
| const sandbox = await createSandbox(); | ||
| return NextResponse.json(sandbox, { status: 200, headers: getCorsHeaders() }); | ||
| ||
| } catch (error) { | ||
| const message = error instanceof Error ? error.message : "Failed to create sandbox"; | ||
| return NextResponse.json({ error: message }, { status: 400, headers: getCorsHeaders() }); | ||
| ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Handler extracts accountId for authentication but never uses it, making the purpose of the validation unclear and suggesting incomplete implementation