Twitter integration with Langchain to enable agentic workflows using the core primitives defined in cdp-agentkit-core
.
This toolkit contains tools that enable an LLM agent to interact with Twitter. The toolkit provides a wrapper around the Twitter (X) API, allowing agents to perform social operations like posting text.
- Python 3.10 or higher
- Node.js 18 or higher
pip install twitter-langchain
npm install @coinbase/twitter-langchain
Set the following environment variables:
export OPENAI_API_KEY=<your-openai-api-key>
export TWITTER_API_KEY=<your-api-key>
export TWITTER_API_SECRET=<your-api-secret>
export TWITTER_ACCESS_TOKEN=<your-access-token>
export TWITTER_ACCESS_TOKEN_SECRET=<your-access-token-secret>
export TWITTER_BEARER_TOKEN=<your-bearer-token>
from twitter_langchain import (
TwitterApiWrapper,
TwitterToolkit
)
# Initialize TwitterApiwrapper
twitter_api_wrapper = TwitterApiWrapper()
# Create TwitterToolkit from the api wrapper
twitter_toolkit = TwitterToolkit.from_twitter_api_wrapper(twitter_api_wrapper)
View available tools:
tools = twitter_toolkit.get_tools()
for tool in tools:
print(tool.name)
import { TwitterAgentkit } from "@coinbase/cdp-agentkit-core";
import { TwitterToolkit } from "@coinbase/twitter-langchain";
// Initialize Twitter AgentKit
const agentkit = new TwitterAgentkit();
// Create toolkit
const toolkit = new TwitterToolkit(agentkit);
// Get available tools
const tools = toolkit.getTools();
The toolkit provides the following tools:
- account_details - Get the authenticated account details
- account_mentions - Get mentions for the account
- post_tweet - Post a tweet to the account
- post_tweet_reply - Post a reply to a tweet on Twitter
import uuid
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage
from langgraph.prebuilt import create_react_agent
llm = ChatOpenAI(model="gpt-4o-mini")
# Create agent
agent_executor = create_react_agent(llm, tools)
# Example - post tweet
events = agent_executor.stream(
{
"messages": [
HumanMessage(content=f"Please post 'hello, world! {uuid.uuid4().hex}' to twitter"),
],
},
stream_mode="values",
)
for event in events:
event["messages"][-1].pretty_print()
Expected output:
================================ Human Message =================================
Please post 'hello, world! c4b8e3744c2e4345be9e0622b4c0a8aa' to twitter
================================== Ai Message ==================================
Tool Calls:
post_tweet (call_xVx4BMCSlCmCcbEQG1yyebbq)
Call ID: call_xVx4BMCSlCmCcbEQG1yyebbq
Args:
tweet: hello, world! c4b8e3744c2e4345be9e0622b4c0a8aa
================================= Tool Message =================================
Name: post_tweet
Successfully posted!
================================== Ai Message ==================================
The message "hello, world! c4b8e3744c2e4345be9e0622b4c0a8aa" has been successfully posted to Twitter!
npm install @langchain/langgraph @langchain/openai
import { ChatOpenAI } from "@langchain/openai";
import { createReactAgent } from "@langchain/langgraph/prebuilt";
// Initialize LLM
const model = new ChatOpenAI({
model: "gpt-4o-mini",
});
// Create agent executor
const agent = createReactAgent({
llm: model,
tools,
});
// Example usage
const result = await agent.invoke({
messages: [new HumanMessage("please post 'hello, world!' to twitter")],
});
console.log(result.messages[result.messages.length - 1].content);
Check out twitter-langchain/examples for inspiration and help getting started!
- Chatbot Python: Simple example of a Python Chatbot that can interact on Twitter (X), using OpenAI.
- Chatbot Typescript: Simple example of a Node.js Chatbot that can interact on Twitter (X), using OpenAI.
See CONTRIBUTING.md for detailed setup instructions and contribution guidelines.