Unable to create a Worker with API

I'm trying to create workers using the API. I found the correct endpoint for this but I'm running into a brick wall by getting this error:
Uncaught SyntaxError: Unexpected token ':'\n at worker.js:2:12\n.
Uncaught SyntaxError: Unexpected token ':'\n at worker.js:2:12\n.
Specific error code is 10021 and it points to validation errors but it does not help at all in this case. https://developers.cloudflare.com/workers/observability/errors/#validation-errors-10021 I'm able to use "List Workers"-endpoint with same credentials with no issues. Do I need some worker.js script? there is no mention of it at all in docs. My current test implementation using Next.js 15:
import { NextResponse } from "next/server";
import Cloudflare from "cloudflare";

export async function PUT(req: Request) {
const { user } = await req.json();

const client = new Cloudflare({
apiToken: process.env.CLOUDFLARE_ACCOUNT_TOKEN!,
});

try {
await client.workers.scripts.update(`${user.id}-builddrr-prod`, {
account_id: process.env.CLOUDFLARE_ACCOUNT_ID!,
metadata: {},
});

return NextResponse.json({ message: "Script updated" }, { status: 200 });
} catch (error) {
return NextResponse.json(
{ error: "Failed to update script", details: error },
{ status: 500 }
);
}
}
import { NextResponse } from "next/server";
import Cloudflare from "cloudflare";

export async function PUT(req: Request) {
const { user } = await req.json();

const client = new Cloudflare({
apiToken: process.env.CLOUDFLARE_ACCOUNT_TOKEN!,
});

try {
await client.workers.scripts.update(`${user.id}-builddrr-prod`, {
account_id: process.env.CLOUDFLARE_ACCOUNT_ID!,
metadata: {},
});

return NextResponse.json({ message: "Script updated" }, { status: 200 });
} catch (error) {
return NextResponse.json(
{ error: "Failed to update script", details: error },
{ status: 500 }
);
}
}
Cloudflare Docs
Errors and exceptions
Review Workers errors and exceptions.
1 Reply
valzu
valzuOP3w ago
Solved this. Appeared to be missing info in docs.

Did you find this page helpful?