puter.workers.create()


⚠️ This is a beta feature. The API may change in future releases.

Creates and deploys a new worker from a JavaScript file containing router code.

To create a worker, you'll need a Puter account with a verified email address.
After a worker is created or updated, full propagation may take between 5 to 30 seconds to fully take effect across all edge servers.

Syntax

puter.workers.create(workerName, filePath)

Parameters

workerName (String)(Required)

The name for the worker. It can contain letters, numbers, hyphens, and underscores.

filePath (String)(Required)

The path to a JavaScript file in your Puter account that contains your router code.

Workers cannot be larger than 10MB.

Return Value

A Promise that resolves to an object on success:

{
    success: true,
    url: "https://worker-name.puter.work",
    errors: []
}

On failure, throws an Error with the reason.

Examples

Basic Syntax

// Create a new worker from a file in your Puter account
puter.workers.create('my-api', 'api-server.js')
    .then(result => {
        console.log(`Worker deployed at: ${result.url}`);
    })
    .catch(error => {
        console.error('Deployment failed:', error.message);
    });

Complete Example

<html>
<body>
    <script src="https://js.puter.com/v2/"></script>
    <script>
    (async () => {
        // 1. Create a worker file in your Puter account.
        puter.print('→ Writing the worker code to my-worker.js<br>');
        const workerCode = `
        // A router for /api/hello
        router.get('/api/hello', async (event) => {
            return 'Hello from worker!';
        });
        `;

        // Save the worker code to my-worker.js in your Puter account
        await puter.fs.write('my-worker.js', workerCode);

        // 2. Deploy the worker using the file path
        const workerName = puter.randName();
        puter.print(`→ Deploying ${workerName} worker. May take up to 10 seconds to deploy.<br>`);
        const deployment = await puter.workers.create(workerName, 'my-worker.js');
        
        // 3. Test the worker
        puter.print(`→ Wait 5 seconds before testing the worker to make sure it's propagated.<br>`);

        setTimeout(async ()=>{
            const response = await fetch(`${deployment.url}/api/hello`);
            puter.print('→ Test response: ', await response.text());
        }, 5000);
    })();
    </script>
</body>
</html>