Introduction
This is a minimal example that uses Tweepy in Google Colab to publish one post to your own account with X API v2. Access, pricing, and authentication requirements can change, so check the official documentation and Developer Console before running it.
Important: credentials in the previous version
An earlier version of this article included API keys and access tokens in its text. Treat any credentials that were published as compromised. If those values were real and active, revoke the affected keys and tokens in the X Developer Console and issue new ones. Copies may remain in repository history, so removing them from the current page alone is not sufficient.
Before you start
- Create a Project and App in the X Developer Console, then create user credentials with permission to post. For OAuth 1.0a User Context, configure read and write permissions for the App before generating access tokens. With OAuth 2.0 PKCE, request the required scopes, including
tweet.write. - Check the current plan, write-endpoint price, and usage limits in the Developer Console. Pricing and access can change; do not rely on old tutorials or amounts quoted in this article.
- Create a notebook in Google Colab. In the Secrets panel (key icon), add four secrets with the names below and allow this notebook to access them. Enter your own App values; do not paste them into notebook code or output.
X_API_KEYX_API_KEY_SECRETX_ACCESS_TOKENX_ACCESS_TOKEN_SECRET
Publish one post
Running this cell publishes the text in text from your X account. Check the account and wording before running it. X API charges may apply.
| |
About automation
This example publishes one post only when you run the cell manually. Before extending it to scheduled posting or automated replies, read X Automation Rules. Automated replies have additional requirements, including a clear indication of intent from the recipient. Colab is not a guaranteed always-on job runner, and its runtimes and usage limits are subject to change.
![Featured image of post Post with the X API and Google Colab [Python]](/p/twitter-api%E3%81%A8google-colaboratory%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A6tweet%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95/img_hu11558181633372417410.webp)