Back to Puter

Workers Exec - Authenticated Requests

src/docs/src/playground/examples/workers-exec.html

26.08.12.7 KB
Original Source

Workers Exec - Authenticated Requests

Test authenticated requests to worker endpoints using puter.workers.exec().

Setup

First, create a test worker with the following code:

// Test worker with authentication router.get('/api/hello', async (event) => { const authHeader = event.request.headers.get('puter-auth'); return { message: 'Hello from authenticated worker!', authenticated: !!authHeader, timestamp: new Date().toISOString() }; }); router.get('/api/user-profile', async (event) => { const authHeader = event.request.headers.get('puter-auth'); if (!authHeader) { return new Response(JSON.stringify({ error: 'Authentication required' }), { status: 401, headers: { 'Content-Type': 'application/json' } }); } return { user: 'authenticated-user', profile: { name: 'John Doe', email: '[email protected]' }, timestamp: new Date().toISOString() }; }); router.post('/api/data', async (event) => { const authHeader = event.request.headers.get('puter-auth'); if (!authHeader) { return new Response(JSON.stringify({ error: 'Authentication required' }), { status: 401, headers: { 'Content-Type': 'application/json' } }); } const body = await event.request.json(); return { received: body, saved: true, timestamp: new Date().toISOString() }; }); router.put('/api/settings', async (event) => { const authHeader = event.request.headers.get('puter-auth'); if (!authHeader) { return new Response(JSON.stringify({ error: 'Authentication required' }), { status: 401, headers: { 'Content-Type': 'application/json' } }); } const body = await event.request.json(); return { updated: body, success: true, timestamp: new Date().toISOString() }; }); router.delete('/api/resource/:id', async (event) => { const authHeader = event.request.headers.get('puter-auth'); if (!authHeader) { return new Response(JSON.stringify({ error: 'Authentication required' }), { status: 401, headers: { 'Content-Type': 'application/json' } }); } const id = event.params.id; return { deleted: id, success: true, timestamp: new Date().toISOString() }; }); router.get('/api/health', async (event) => { return { status: 'healthy', uptime: Date.now(), timestamp: new Date().toISOString() }; }); router.options('/*page', ({request}) => { return new Response(null, { status: 204, headers: { 'Access-Control-Allow-Origin': request.headers.get('origin'), 'Access-Control-Allow-Headers': '*', 'Access-Control-Allow-Credentials': 'true', 'Access-Control-Allow-Methods': '*' } }); }); Worker Name: Create Test Worker

Request Builder

GETPOSTPUTDELETE

Endpoint:

Request Body (JSON):

Headers (optional): Execute RequestTest All Endpoints

Quick Tests

Test HelloTest User ProfileTest POST DataTest PUT SettingsTest DELETE ResourceTest Health

Batch Operations

Run Batch TestsTest with Timeout