Skip to content
← Public packages

@cameronpak/box

Full Box Public API v1 client: lifecycle, prompts, files, commands, snapshots, environments, webhooks, desktop, hosting, and account.

src/create-box.ts

44 lines · 1.7 KB · TypeScript
import { boxRequest, type Result } from './client.ts'
import type { Box, BoxSize } from './types.ts'

export type CreateBoxInput = {
	/** Machine size. small is 2 vCPU / 4 GB, default is 4 / 8, large is 8 / 16. */
	type?: BoxSize
	/** Seconds before automatic archival. Pass null to disable auto-stop. Defaults to 3600. */
	ttlSeconds?: number | null
	/** Per-box environment variables layered over the account environment. */
	env?: Record<string, string>
	/** Withhold every account secret from the box. Use for boxes you hand to other people. */
	noEnv?: boolean
	/** Named environment to attach. Omit to use the account default. */
	environment?: string
	/** Shell script that starts after the box is ready. Never blocks ready. Max 64KB. */
	setupScript?: string
	/** Create from a named snapshot saved with saveNamedSnapshot. */
	from?: string
}

export type CreateBoxOutput = {
	status: string
	ttlSeconds: number | null
	box: Box
}

/**
 * Provision a new Box. The box is not usable yet; poll with waitForBox.
 * Store the returned box.id with whatever job or session record owns this work.
 */
export default async function createBox(
	input: CreateBoxInput = {},
): Promise<Result<CreateBoxOutput>> {
	const body: Record<string, unknown> = {}
	if (input.type !== undefined) body.type = input.type
	if ('ttlSeconds' in input) body.ttlSeconds = input.ttlSeconds
	if (input.env !== undefined) body.env = input.env
	if (input.noEnv !== undefined) body.noEnv = input.noEnv
	if (input.environment !== undefined) body.environment = input.environment
	if (input.setupScript !== undefined) body.setupScript = input.setupScript
	if (input.from !== undefined) body.from = input.from

	return boxRequest<CreateBoxOutput>({ path: '/boxes', method: 'POST', body })
}