This template provides a foundation for building your own Farcaster agent using Cloudflare Workers. It includes a built-in memory system, character customization, and an extensible action framework.
- Neynar developer account
- Cloudflare account
- OpenRouter account
- Node.js and npm installed
- Create a new Cloudflare Worker project
npm create cloudflare@latest my-farcaster-agent
cd my-farcaster-agent
- Copy Template Files
- Copy all contents from this template directory into your worker's
src
directory
- Install Dependencies
npm install
Create a wrangler.toml
file in your project root:
name = "farcaster-agent"
main = "src/index.js"
compatibility_date = "2023-01-01"
node_compat = true
[vars]
FARCASTER_FID = "your_fid"
FARCASTER_NEYNAR_SIGNER_UUID = "your_signer_uuid"
FARCASTER_NEYNAR_API_KEY = "your_neynar_key"
OPENROUTER_API_KEY = "your_openrouter_key"
# KV namespace binding
[[kv_namespaces]]
binding = "AGENT_KV"
id = "your_kv_namespace_id"
# Create the KV namespace
npx wrangler kv:namespace create AGENT_KV
Add the returned KV namespace ID to your wrangler.toml
.
Edit src/config/character.json
to define your agent's personality:
{
"name": "YourAgent",
"bio": [
"A knowledgeable AI agent on Farcaster",
"Specializes in [your specialty]"
],
"style": {
"tone": [
"friendly but professional",
"technically accurate"
],
"writing_style": [
"use clear explanations",
"maintain conversation context"
]
},
"system_prompt": "You are [name], [key characteristics]..."
}
- Create a Farcaster account through Neynar's API
- Deploy your worker:
npx wrangler deploy
- In the Neynar dashboard:
- Go to the webhooks tab
- Create a new webhook
- Enter your worker URL
- Add your bot's FID to both
mentioned_fids
andparent_author_fids
The agent uses an extensible action system. Here's how to add your own action:
- Create a new file in the
actions
directory (e.g.,myaction.js
):
import { BaseAction } from './base';
export class MyAction extends BaseAction {
constructor() {
super('myaction'); // Command name users will type
}
async execute(cast, context) {
// Your action logic here
return {
success: true,
response: "Action completed!"
};
}
}
- Register your action in
actions/index.js
:
import { MyAction } from './myaction';
const actions = {
myaction: new MyAction()
};
export function loadActions() {
return actions;
}
The agent includes a two-tier memory system:
- Conversation Memory: 24-hour TTL
- Long-term Memory: 30-day TTL
Memory is automatically managed through Cloudflare KV.
- Test locally with
npx wrangler dev
- Monitor logs with
npx wrangler tail
- Use environment variables for all sensitive keys
- Start with simple actions and gradually add complexity
Required environment variables:
FARCASTER_FID
: Your Farcaster IDFARCASTER_NEYNAR_SIGNER_UUID
: UUID from Neynar dashboardFARCASTER_NEYNAR_API_KEY
: Neynar API keyOPENROUTER_API_KEY
: OpenRouter API key
# Deploy your worker
npx wrangler deploy
# Set environment variables
npx wrangler secret put FARCASTER_NEYNAR_API_KEY
npx wrangler secret put OPENROUTER_API_KEY
MIT License