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.wrangler.jsonc. The nodejs_compat flag enables the Node.js APIs used by the SDK dependencies.
wrangler.jsonc
.gitignore before creating your secrets file:
.gitignore
.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
env argument. The Worker passes those values directly to each SDK client.
Write the Worker
Createsrc/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:tsconfig.json to use those types:
tsconfig.json
AGENT_TOKEN to the value from .dev.vars and invoke the agent:
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:
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.workers.dev URL printed by Wrangler to call the deployed endpoint with the same header:
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
- OpenAI Worker entry point and Hacker News agent use a bounded Chat Completions loop.
- Mastra Worker entry point and Hacker News agent let Mastra run the tool loop. The agent passes the Worker binding to
createOpenAI({ apiKey }).