-
Notifications
You must be signed in to change notification settings - Fork 6
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
Merged
sweetmantech
merged 5 commits into
test
from
sweetmantech/myc-4023-api-create-sandbox-post-sandbox
Jan 23, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3d56415
feat: add Google Drive and Google Docs to enabled toolkits (#153) (#154)
sidneyswift 4d894c4
Merge remote-tracking branch 'origin/main' into test
sweetmantech a314b24
feat: add POST /api/sandbox endpoint
sweetmantech 7ef36d5
fix: add snapshotting status to SandboxCreatedResponse
sweetmantech c96bac3
refactor: use Sandbox SDK types for SandboxCreatedResponse
sweetmantech File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(), | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() }); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } catch (error) { | ||
| const message = error instanceof Error ? error.message : "Failed to create sandbox"; | ||
| return NextResponse.json({ error: message }, { status: 400, headers: getCorsHeaders() }); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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