DEV Community

Jurijs Ivolga
Jurijs Ivolga

Posted on • Originally published at cyberpunk.tools

1

How to Create a Widget for Meta Threads using Scriptable

I recently started using Meta Threads and saw someone complaining that Meta should create a widget for certain stats. I replied, saying it should be possible using the Scriptable app. Long story short, I decided to dive into this rabbit hole and create some widgets for Meta Threads.

I'll be creating two widgets:

  • Main widget: Follower counts and profile visitors (today and yesterday)
  • Secondary widget: View counts for the latest post

We'll use the Scriptable app, which you can download free from the iOS App Store.

Meta Threads Widget Example

Step 1: Set Up the Meta App

Go to Meta for Developers and create an app. The setup process is mostly straightforward ("next-next-next").

Select "I don’t want to connect a business portfolio yet."

Setup

Then choose "Access the Threads API".

Threads API

Enter your app name and email:

App details

Click "Go to Dashboard".

In dashboard settings, continue with more straightforward steps.

First, select permissions under "Access the Threads API"—specifically threads_basic and threads_manage_insights:

Permissions

Then click "Test Use Cases", followed by "Finish Customization".

Step 2: Add Roles

Navigate to "App Roles" → "Roles".

Roles

Click "Add People", select "Threads Tester", and add yourself:

Add tester

Open the Threads app with your user account, then navigate to Settings → Account → Website Permissions → Invites and accept the invite:

Accept invite

Step 3: Get an Access Token

Visit Facebook Developer Explorer and select "threads.net", then click "Generate Threads Access Token".

Click "Continue" in the pop-up. Your token will appear in the Access Token field.

Validate your token with the Access Token Debugger to ensure it works correctly.

Step 4: Exchange for a Long-Lived Token

Exchange your short-lived token (expires in 60 minutes) for a long-lived token (valid for two months). Follow instructions from Meta Documentation on Long-Lived Tokens using this command:

curl -s -X GET "https://graph.threads.net/access_token?grant_type=th_exchange_token&client_secret=<THREADS_APP_SECRET>&access_token=<SHORT_LIVED_ACCESS_TOKEN>"
Enter fullscreen mode Exit fullscreen mode

To find your THREADS_APP_SECRET, go to your app's settings (App Settings → Basic) on Facebook Apps Dashboard:

App secret

Use this secret in the curl command above. Recheck your new long-lived token with the Access Token Debugger.

Step 5: Save Your Long-Lived Token to iCloud

Our scripts will need to access your token and refresh it too. First, we need to store your token in iCloud, so later our scripts can use it. Run this script just once with Scriptable, and later the main widget script will handle future token refreshes automatically.

Token Storage Script (Gist)

Step 6: Building Your Widgets

We'll build two widgets using Scriptable:

  • A main widget displaying followers count and profile visitors (today/yesterday).
  • A secondary widget showing view counts of your latest post.

Main Widget Endpoints:

  • Followers: https://graph.threads.net/v1.0/me/threads_insights?metric=followers_count&access_token=YOUR_TOKEN
  • Profile Visitors: https://graph.threads.net/v1.0/me/threads_insights?metric=views&access_token=YOUR_TOKEN

Full code:
Main Widget Code (Gist)

Secondary Widget Endpoints:

Fetch latest threads: https://graph.threads.net/v1.0/me/threads?fields=id,text,views&access_token=YOUR_TOKEN
Retrieve stats for latest thread:https://graph.threads.net/v1.0/${postId}/insights?metric=likes,replies,views&access_token=YOUR_TOKEN

Full code:
Secondary Widget Code (Gist)

Originally published at Cyberpunk Tools Blog.

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay