Skip to main content
Archived

This project has been archived by its maintainers, and is no longer receiving any updates.

Twitter (X) Langchain Toolkit

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.

Setup

Prerequisites

Installation

pip install twitter-langchain

Environment Setup

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>

Usage

Basic Setup

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)

Available Tools

The toolkit provides the following tools:

  1. account_details - Get the authenticated account details
  2. account_mentions - Get mentions for the account
  3. post_tweet - Post a tweet to the account
  4. post_tweet_reply - Post a reply to a tweet on Twitter

Using with an Agent

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!

Examples

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.

Contributing

See CONTRIBUTING.md for detailed setup instructions and contribution guidelines.

Metadata

Release files for twitter-langchain 0.0.11

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for twitter-langchain 0.0.11
File Size Uploaded
twitter_langchain-0.0.11.tar.gz 4.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for twitter-langchain 0.0.11
File Interpreter ABI Platform
twitter_langchain-0.0.11-py3-none-any.whl Python 3 none any Details

Total release size: 11.0 kB

Release files / twitter_langchain-0.0.11.tar.gz

Download URL twitter_langchain-0.0.11.tar.gz
Size 4.8 kB
Tags Source
SHA-256 checksum
How to use checksums
34445bbc0bcd2684197b917aff8e5e5f3db164d38e754fb67f6dc2359ca194c3
BLAKE2b-256 checksum
How to use checksums
881d8efcd67a20861c2f3860f1bd6a3f97b80f1701e6b501d4f1b6553904475f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.8

Release files / twitter_langchain-0.0.11-py3-none-any.whl

Download URL twitter_langchain-0.0.11-py3-none-any.whl
Size 6.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
42657011c4752066862bf4de2bd36e69913654ea02bab6d9f995ce40793db461
BLAKE2b-256 checksum
How to use checksums
7ad116c0d5d07718a96954588cfc9cf6734472eb55c212b5199b6ad84b95ad8a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.8
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page