In this tutorial, discover how the Twitter API can enhance your business, and learn how to post and get analytics directly from your platform or app on behalf of your users.
Despite its recent upheaval, Twitter remains one of the most visible social media platforms with about , curation lists, direct messages, website embeds, and from a business they follow. Other studies show that
Twitter’s user demographics are also compelling. The number of monetizable users in the U.S. has enable web and mobile applications to use Twitter data and features for their unique business needs and customer experiences.
Here are a few interesting business use cases for a Twitter API:
- A hiring platform (like Indeed) can post new jobs from a client on that client’s Twitter account.
- A home real estate platform (like Zillow) can publish videos of new rentals on the landlord’s Twitter account and their ads using the ads API.
- Whenever a shopping season hashtag like “#blackfridaysale” is trending, an e-commerce platform like Shopify can automatically detect it in real-time and tweet the latest products in its inventory.
- A customer service platform like Intercom can automatically search tweets using the search endpoint, and open tickets for a customer who receives user complaints on Twitter.
If you’re a developer, we’ll walk you through a step-by-step tutorial to start using the Twitter APIs.
Prepare To Use The Twitter API
First, you have to figure out what your app needs, and then configure its details in Twitter’s developer portal.
1. Select the Features and API Versions You Need
Twitter APIs have multiple versions like Twitter API v2, Standard v1.1, and Enterprise APIs (or Gnip). Use the : Use Twitter API v2, which is based on page. Each level unlocks more features and higher rate limits. You get essential access by default without needing any approval. But approval is required for elevated and academic research access. In general, essential access is sufficient to start, and you can apply for elevated access when needed. Note that approval can take up to two weeks or more.
3. Register With the Developer Portal
Sign up for a developer account on the
On logging in, you’ll see your main app under “Projects & Apps.” Select the app and click “Set up” under “User authentication settings.” Configure your app as follows:
App permissions: Read and write.
Type of App: If your app can send secrets like the client secret securely, select “Confidential client.” If not, select “Public client.” Typically, traditional client-server web apps are confidential while mobile apps without any servers are public clients.
Callback URI: Register the endpoints of your app that Twitter can call during user authorization.
Website URL: A link to your web application or its information page.
5. Store the Client ID and Secret for Your App
After configuration, you’ll get a client ID and client secret for your app. Store them securely in a database, vault, or similar. Either design your application to read them securely, or set up your server to read and inject them into your application’s environment.
6. Decide on Direct Calls or a Client SDK Package
You can either make all the Twitter REST API calls directly or use one of the many since it supports most features (v1.1 and v2) and is maintained. We also recommend
In your /authorize endpoint, create an authorization URL and redirect your user there. It’ll open an authorization page owned by Twitter.
Authorization URL format:
https://twitter.com/i/oauth2/authorize?response_type=code&scope=tweet.read%20tweet.write%20users.read%20offline.access&client_id={client-id}&redirect_uri={registered-url}&state={state}&code_challenge={challenge}&code_challenge_method=S256
The important parameters here are:
redirect_uri={registered-url}: An endpoint of your web or native app that’s also registered in the developer portal. Twitter sends the authorization code here.
client_id={client-id}: Your app’s client ID from the developer portal.
scope: The permissions your app needs. Though you just want permission to post, the posting endpoint itself endpoint by specifying the information you want intweet.fields:
CODEGET https://api.twitter.com/2/tweets/{ID}?tweet.fields=public_metrics,organic_metrics,non_public_metrics
{ID}: A tweet ID
tweet.fields: One or more of public_metrics, organic_metrics, non_public_metrics, and promoted_metrics. The .
Posting a tweet using our API’s is as easy as this:
CODE// Install Ayrshare’s Node package: npm i social-post-api
import SocialPost from 'social-post-api';
// 1. Get your API key from https://app.ayrshare.com/api.
// 2. Add it to your application’s environment.
// export AYRSHARE_API_KEY=YOUR-AYRSHARE-API-KEY
const social = new SocialPost(process.env.AYRSHARE_API_KEY);
const post = social.post({
"post": "Your tweet",
"platforms": ["twitter"]
})
.then((json) => console.log(json))
.catch(console.error);
The prerequisites are also simple. Just , and start calling. You don’t have to struggle with OAuth, app settings, credential security, approval, or Twitter’s docs.
such as Python, Airtable, and Bubble.
1. Tweet With Ayrshare, Node, and Fetch
You don’t have to use our NPM package or other integrations. Calling our API using your preferred HTTP package is just as simple! Here’s an example using the
node-fetchpackage:
CODEconst params = {
'post': 'Your tweet',
'platforms': ['twitter']
}
const apiResp = await fetch('https://app.ayrshare.com/api/post', {
method: 'POST',
headers: {
'Authorization' : Bearer ${process.env.AYRSHARE_API_KEY},
'Content-Type': 'application/json'
},
body: JSON.stringify(params)
});
const tweet = await apiResp.json();
2. Post a Twitter Thread With Ayrshare, Python, and Requests
Posting a numbered thread of tweets requires just two additional options. Ayrshare automatically breaks up your lengthy message into a numbered sequence of tweets. Compare this to the Twitter API where you have to post each tweet with details of its previous tweet.
CODEimport os
import requests
params = {
'post': 'Your lengthy essay…',
'platforms': ['twitter'],
'twitterOptions': {
'thread': True,
'threadNumber': True
}
}
headers = {
'Authorization': f'Bearer {os.environ["AYRSHARE_API_KEY"]}',
'Content-Type': 'application/json'
}
r = requests.post('https://app.ayrshare.com/api/post',
json=params, headers=headers)
tweet = r.json()
3. Tweet an Image or Video Using Ayrshare’s Python Package
For images and videos in your user’s tweets, Twitter API expects you to upload them to its .
Using Ayrshare, just set the
mediaUrlsoption to a list of links.
Remember that you need a premium, business, or enterprise plan to tweet videos or more than one image.
If you don’t have a server for your media, you can upload them to our for the full response.
Boost Your Users’ Online Presence With Twitter API Posting
As a powerful social media platform, Twitter can benefit your customers and drive business. Moreover, by learning the basics of both Twitter and Ayrshare APIs, you can further improve engagement.
For more in-depth information about Twitter, check out our .
↗ Original-Artikel auf dev.to lesenVollständiger Original-BerichtAusführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
SOCIAL SHARE CARD GENERATOR