Skip to main content
Build a Cloudflare Worker that returns the top three Hacker News stories. The Worker uses OpenAI’s Responses API and a Composio session scoped to the public hackernews toolkit. You don’t need to connect a Hacker News account. The SDK repository includes OpenAI and Mastra Workers examples. This walkthrough builds a standalone OpenAI project and adds a bearer token so only you can invoke the agent.

Create the project

You need Node.js 22.22.3 or later, a Composio API key, and an OpenAI API key. You also need a Cloudflare account to deploy.
Create wrangler.jsonc. The nodejs_compat flag enables the Node.js APIs used by the SDK dependencies.
wrangler.jsonc
Add these entries to .gitignore before creating your secrets file:
.gitignore
Create .dev.vars next to wrangler.jsonc. Replace both API key placeholders and generate a separate token with openssl rand -hex 32 for AGENT_TOKEN.
.dev.vars
Wrangler loads local secrets into the handler’s env argument. The Worker passes those values directly to each SDK client.

Write the Worker

Create src/index.ts with the complete handler below. Each authenticated POST / creates a session for a server-owned identity and runs the same Hacker News task.
src/index.ts
handleToolCalls executes each tool through the session that supplied it. The loop sends the results back to OpenAI and stops when the model returns text, with a limit of ten model turns. Connection management and the remote sandbox are disabled because this task only reads public Hacker News data. The handler awaits the agent result and deletes the request’s session before returning, including when the agent fails. If deletion fails, it logs the error and preserves the response. ctx.waitUntil(composio.flush()) lets Composio finish sending telemetry after the response. Cloudflare gives waitUntil up to 30 seconds after the response or a client disconnect, so keep the agent run in the awaited request path.

Check and run locally

Generate Worker types from your Wrangler configuration:
Create tsconfig.json to use those types:
tsconfig.json
Check the TypeScript and Worker bundle, then start the local server:
In a second terminal, set AGENT_TOKEN to the value from .dev.vars and invoke the agent:
The response contains three current story titles and links. Local requests still call Composio and OpenAI. If you receive 502, read the error in the terminal running Wrangler and check both API keys. Check that requests without the token are rejected before either SDK runs:
The response is 401 Unauthorized.

Deploy

Log in to Cloudflare, then set the deployed Worker’s secrets. Paste each value when Wrangler prompts you. If Wrangler offers to create the Worker on the first secret command, accept.
Use the workers.dev URL printed by Wrangler to call the deployed endpoint with the same header:
Keep AGENT_TOKEN in a trusted client or backend. If you expand this into a multi-user app, authenticate each person and derive their Composio user ID on the server. See how sessions scope users and tools before adding tools that access connected accounts.

Explore the repository examples

Those entry points are runtime examples. Add the authentication check above before exposing them as a deployed endpoint. For framework setup, read the OpenAI provider guide or the Mastra provider guide.