A Replit template for running an AI agent with onchain capabilities and X posting using the Coinbase Developer Platform (CDP) Agentkit.
Supports CDP Agentkit actions + our own actions, allowing it to perform blockchain operations like:
- Deploying tokens (ERC-20 & NFTs)
- Managing wallets
- Executing transactions
- Interacting with smart contracts
- Posting on X
- Finding news about a specific token
- Browsing like a human about a specific thing if there's no API
- API Keys
- OpenAI API key from the OpenAI Portal
- CDP API credentials from CDP Portal
- X Social API (Account Key and secret, Access Key and Secret)
- Crypto compare API key CRYPTO_COMPARE_API_KEY
- Fill ENV Fill this
OPENAI_API_KEY=""
CDP_API_KEY_PRIVATE_KEY=""
CDP_API_KEY_NAME=""
TWITTER_API_KEY=""
TWITTER_API_SECRET=""
TWITTER_ACCESS_TOKEN=""
TWITTER_ACCESS_TOKEN_SECRET=""
NETWORK_ID="base-sepolia"
TWITTER_BEARER_TOKEN="fake"
CRYPTO_COMPARE_API_KEY=""
MORALIS_API_KEY=""
The agent exposes a REST API that allows real-time interaction with streaming responses. The API is built using FastAPI and provides detailed information about the agent's actions and thought process.
http://localhost:8000
POST /chat
Request Body:
{
"message": string, // The message to send to the agent
"stream": boolean // Whether to stream the response (default: false)
}
Response Format:
For non-streaming requests (stream: false
):
{
"response": string // The agent's complete response
}
For streaming requests (stream: true
), you'll receive a series of Server-Sent Events (SSE) with the following types:
- Thinking State:
{
"type": "thinking",
"content": "Processing your request...",
"step": "start"
}
- Tool Usage:
{
"type": "tool_usage",
"content": string, // Description of the tool being used
"tool_type": string // Type of tool: "external_api", "internal", or "unknown"
}
- Response:
{
"type": "message",
"content": string, // The agent's response
"step": "response"
}
- Error (if any):
{
"type": "error",
"content": string, // Error message
"step": "error"
}
GET /health
Response:
{
"status": "healthy",
"agent_initialized": boolean,
"config_loaded": boolean
}
// Using fetch
const response = await fetch('http://localhost:8000/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
message: "What are the current top cryptocurrency exchanges?",
stream: false
})
});
const data = await response.json();
console.log(data.response);
// Using EventSource
const sse = new EventSource('/chat');
const response = await fetch('http://localhost:8000/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
message: "What are the current top cryptocurrency exchanges?",
stream: true
})
});
// Handle the streaming response
const reader = response.body.getReader();
const decoder = new TextDecoder();
while (true) {
const {value, done} = await reader.read();
if (done) break;
const chunk = decoder.decode(value);
const messages = chunk.split('\n').filter(Boolean);
for (const message of messages) {
const data = JSON.parse(message);
switch (data.type) {
case 'thinking':
console.log('Agent is thinking:', data.content);
break;
case 'tool_usage':
console.log('Using tool:', data.content);
break;
case 'message':
console.log('Agent response:', data.content);
break;
case 'error':
console.error('Error:', data.content);
break;
}
}
}
- Install dependencies:
poetry install
- Start the server:
poetry run uvicorn api:app --reload
The API will be available at http://localhost:8000
. You can access the interactive API documentation at http://localhost:8000/docs
.
With pip:
pip install browser-use install playwright:
playwright install
-
Configure Secrets and CDP API Keys Navigate to Tools > Secrets and add the secrets above.
-
Run the Bot
- Click the Run button
- Choose between chat mode or autonomous mode
- Start interacting onchain!
Every agent comes with an associated wallet. Wallet data is read from wallet_data.txt, and if that file does not exist, we will create a new wallet and persist it in a new file. Please note that this contains sensitive data and should not be used in production environments. Refer to the CDP docs for information on how to secure your wallets.
- Interactive chat mode for guided interactions
- Autonomous mode for self-directed blockchain operations
- Full CDP Agentkit integration
- Persistent wallet management
- RESTful API with streaming support
This template is based on the CDP Agentkit examples. For more information, visit: https://github.com/coinbase/cdp-agentkit