Example code playground built with Next.js, Monaco, and Freestyle Serverless Runs. It lets users pick a snippet, edit TypeScript in the browser, and execute that code server-side while seeing the returned result and console logs in the same UI.
The implementation is based on the accompanying guide in GUIDE.md and the published Freestyle docs article:
- Embeds Monaco in a React client component for an in-browser TypeScript editor
- Fetches npm type definitions at runtime so IntelliSense works for packages like
freestyle - Ships a small snippet library that users can switch between instantly
- Executes user code with
freestyle.serverless.runs.create - Displays structured run results plus normalized console output
- Next.js App Router
- React
@monaco-editor/reactfreestyle
Install dependencies:
npm installCreate a local environment file and add your Freestyle API key:
cp .env .env.localThen set:
FREESTYLE_API_KEY=your_key_hereStart the development server:
npm run devOpen http://localhost:3000.
- The browser sends the current TypeScript snippet to a Next.js server action.
- The server action creates a Freestyle Serverless Run with that source code.
- The run executes in an isolated environment with the
freestylepackage available. - Outbound requests to
api.freestyle.shget the real API key injected server-side through egress transforms. - The UI renders the returned
resultand any capturedlogs.
This keeps credentials off the client while still letting users experiment with real API behavior.
app/
actions/
executeSnippetAction.ts
getNpmTypeDefsAction.ts
components/
Playground.tsx
lib/
examples.ts
formatRunLogs.ts
page.tsx
GUIDE.md
Key files:
app/components/Playground.tsxwires together Monaco, snippet selection, execution state, and the output panel.app/actions/getNpmTypeDefsAction.tsfetches.d.tsfiles from jsDelivr so Monaco can provide autocomplete for npm packages.app/actions/executeSnippetAction.tsruns user code through Freestyle Serverless Runs.app/lib/examples.tsstores the built-in snippets.app/lib/formatRunLogs.tsnormalizes mixed log values into readable strings.
- The playground currently registers types for the
freestylepackage at version0.1.49. - Monaco waits for both theme selection and type definitions before rendering, which avoids a wrong-theme flash and missing IntelliSense on first load.
- The example snippets demonstrate both nested serverless runs and VM creation.
npm run devstarts the local dev server.npm run buildcreates a production build.npm run startruns the production server.npm run lintruns ESLint.
