Threads API is here

Threads API is here

I’ll show you how to use the Threads API to generate posts automatically 🎉

As of today, the Threads API is available and I’ll show you how to use it to automatically provide content there.

Prerequisites

You definitely need to have a developer account with Meta. You can easily create one here:

Meta for DevelopersWith Facebook for Developers, you can develop code that connects people. Explore artificial intelligence, business tools, gaming, open source, publishing, social hardware, social integration and virtual reality. Learn more about Facebook’s global developer training and networking programs.

In addition, your account must be verified as a business account. You must do this in advance, otherwise you will not have access. For me, it was enough to submit the relevant documents and I was rejected after 1-2 days, but an objection later meant that I was verified and can now create business apps.

Business app is mandatory ☝🏼

The API is aimed at business customers so that the workflows can be opened and content can be cross-posted. That’s why you have to create a business app. You can do this quite easily in the app dashboard. It must also be a new app, as old apps do not have threads integrated. In the functions of the new app, select “Access the Threads API”:

You then enter an app name and your email address. You then have to confirm this once with your meta password and off you go.

Set permissions 🔏

You then have to set the appropriate permissions for your app. If you only want to post, then threads_basic and threads_content_publish will probably be sufficient for you:

You can add more permissions later. For example, you can also check replies, automatically clean up or many other things. These will be needed later as SCOPES.

Configure OAuth 🤝

In order for you (or possibly other people) to be able to use your app, you need to configure URLs that are used by OAuth. OAuth is an authorization process that works completely password-free and thus authorizes users on the resource’s system.

If you don’t have your own website, you can use Postman here. Postman simplifies the use of APIs immensely and can also handle the entire OAuth flow for you. If you already have Postman, you can enter this URL in the Redirect Callback URLs: https://oauth.pstmn.io/v1/callback. Alternatively, any other URL that can receive the authorization codes from OAuth.

Add tester 🤵🏼

If you only want to invite certain people, you can also add certain testers. However, you don’t need to do this in this case, as we request the rights directly via OAuth and link threads to your new app.

It makes perfect sense to select certain people who have test access to your app.

Remember app settings 💡

So that you can now make the API calls against the Threads API, you still need the CLIENT_ID and the CLIENT_SECRET. You need to pay particular attention to the secret, as it can be used to access your app. Be careful where you enter it and ideally do not pass it on to anyone. You can view both values under App settings and then Basic:

We authorize ourselves

Query authorization code 𢝓

Now that we have met all the requirements, we can create the first requests to the Threads API. In order to have access to the resources in the first place, we need to request an authorization code. This is done via a URL that we have to enter in the browser or open for the user. This URL looks like this:

https://threads.net/oauth/authorize?
client_id=[CLIENT ID]&
redirect_uri=[REDIRECT URL]
&scope=[FORGIVEN SCOPES]
&response_type=code

Once you have called up the query in the browser, a page appears that informs you of a link request and lets you check this again and accept it:

If you continue, the authorization code will be passed to the REDIRECT URL (which you have configured in the app’s OAuth). A return would look like this:

{
“code”: “Your authorization code”
}

You can then use this code to request an access token.

Request a short-lived access token 🔑

In order for you to have access to the resources, you must have the authorization code converted into an access token. There are two types of tokens:

Short-lived access tokens
Long-lived access tokens

Both have their advantages and disadvantages. Since you probably want to have access for longer than just a few hours, let’s get a long-lasting access token. To do this, however, we first need a short-lived one, which we then convert to a long-lived one.

To get a short-lived access token, you need to send the authorization code to the API:

curl -X POST
https://graph.threads.net/oauth/access_token
-F client_id=[CLIENT ID]
-F client_secret=[CLIENT SECRET]
-F grant_type=authorization_code
-F redirect_uri=[REDIRECT URL]
-F code=[AUTH CODE]

If everything went well, you should receive a return like this:

{
“access_token”: “THQVJ…”,
“user_id”: xyz
}

You should now also save both. You need the access token for every request to the API, as well as the user_id.

Create a long-lived access token 🧬

Since we now want to use the API for more than just a few hours, you need to convert the short-lived access token into a long-lived one. This is quite simple with the following API call:

curl -i -X GET “https://graph.threads.net/access_token
?grant_type=th_exchange_token
&client_secret=[CLIENT SECRET]
&access_token=[SHORT_LIVED_ACCESS_TOKEN]”

If everything went well, you should get the following return:

{
“access_token”: “<LONG_LIVED_USER_ACCESS_TOKEN>”,
“token_type”: “bearer”,
“expires_in”: 5183944 // number of seconds until token expires
}

You can then remember the access_token instead of the short-lived one and continue to use it.

Renew access token ♻️

Both tokens expire at some point, one sooner and the other later. This is why there is also the option of renewing access tokens before they expire.

To do this, you can simply take a long-lived access token and send it to the API so that it can issue you a new one:

curl -i -X GET “https://graph.threads.net/refresh_access_token
?grant_type=th_refresh_token
&access_token=[LONG_LIVED_ACCESS_TOKEN]”

If that also worked, you can hope for this return:

{
“access_token”: “<LONG_LIVED_USER_ACCESS_TOKEN>”,
“token_type”: “bearer”,
“expires_in”: 5183944 // number of seconds until token expires
}

And you have already created a new valid access token.

Posting content 📷

Now it’s getting exciting. You can now authenticate and authorize yourself. The only thing missing is posting content. Threads offers a wide range of content that can be posted. You can find out which ones are possible in the API documentation.

Posts – Threads API – Documentation – Meta for DevelopersPublish a single image, video, text, or carousel post on Threads.

If we only want to send a simple text or image, we can do this quite elegantly with this API call. To do this, you need your THREADS_USER_ID, which you received above when generating an access token and hopefully saved:

curl -i -X POST
“https://graph.threads.net/v1.0/<THREADS_USER_ID>/threads?media_type=IMAGE&image_url=https://www.example.com/images/bronz-fonz.jpg&text=#BronzFonz&access_token=<ACCESS_TOKEN>”

In this case, we upload an image from a public URL to Threads and add the text #BronzFronz. However, you do not have to upload only images, but can also upload only texts with URLs or similar things.

{
“id”: “1234567” // Threads Media Container ID
}

However, you have only created a media container. Nothing has been uploaded yet! You have to do this separately with a separate API call. For this, you need the id from the previous return:

curl -i -X POST
“https://graph.threads.net/v1.0/<THREADS_USER_ID>/threads_publish?creation_id=<MEDIA_CONTAINER_ID>&access_token=<ACCESS_TOKEN>”

If that worked, you should get a return like this:

{
“id”: “1234567” // Threads Media ID
}

And you should see your new post on Threads now! Congratulations 🎉

But the API can do more than just post. You can also do completely different things with it and pull data from threads for analysis purposes. But just take a look at the API documentation, you’re sure to find some very nice use cases there.

Conclusion 📃

You have now learned how to use the Threads API and can now upload things there automatically. I’ve now shown you how easy it is. It is definitely very appealing for creators or shops/companies to set up workflows and, for example, realize cross-posts for threads.

You will probably have seen this post in Threads now, it was also automatically uploaded via the API 😉

If you like my posts, it would be nice if you follow my Blog for more tech stuff.