Notifications
Access your notification timeline, manage notification settings, and check badge counts.
notifications.timeline(opts?)
Get your notifications timeline. Uses the GraphQL notificationsQuery query.
| Option | Type | Description |
|---|---|---|
opts.count | number | Number of results (default 20) |
opts.cursor | string | Pagination cursor |
opts.timelineType | string | "All" (default), "Mentions" or "Verified" |
opts.variables | object | Additional GraphQL variables |
const notifs = await client.notifications.timeline();
// Fetch more
const more = await client.notifications.timeline({ count: 50 });
// Paginate
const next = await client.notifications.timeline({ cursor: "DAABCgAB..." });notifications.detail(notificationId, opts?)
Expand one grouped notification, like "X and 20 others liked your post", into its users and posts. Uses the GraphQL notificationDetailQuery query. The ID is the notification's id.
notifications.tweets(opts?)
Get the posts from accounts you turned on post notifications for, as parsed tweets. Uses the GraphQL tweetNotificationsQuery query.
const { tweets } = await client.notifications.tweets();notifications.pushSettings()
Get your push notification settings. Uses the GraphQL pushNotificationsSettingsQuery query.
notifications.enableWebNotifications()
Enable logged-out web push notifications. Uses the legacy GraphQL EnableLoggedOutWebNotifications mutation, which the web client no longer calls.
await client.notifications.enableWebNotifications();notifications.saveSettings(params?)
Save notification settings. Uses the v1.1 notifications/settings/save endpoint.
await client.notifications.saveSettings({
// notification setting parameters
});notifications.loginSettings(params?)
Update notification settings on login. Uses the v1.1 notifications/settings/login endpoint.
await client.notifications.loginSettings({
// login notification parameters
});notifications.checkin(params?)
Check in for notification settings. Uses the v1.1 notifications/settings/checkin endpoint.
await client.notifications.checkin();notifications.badge()
Get the unread counts for notifications, chat and Grok, as { ntab_unread_count, xchat_unread_count, ... }. Uses the GraphQL fetchNotificationBadgeCountsQuery query.
const badge = await client.notifications.badge();
console.log(badge);Full example
// Check unread notification count
const badge = await client.notifications.badge();
// Fetch your notifications
const notifs = await client.notifications.timeline();
// Get more notifications with pagination
const page2 = await client.notifications.timeline({
count: 40,
cursor: "DAABCgAB...",
});
// Enable web push notifications
await client.notifications.enableWebNotifications();
// Save custom notification settings
await client.notifications.saveSettings({
// your settings
});notifications.markSeen(cursor, type?)
Mark notifications as read, the same call x.com makes when you open the notifications tab. Uses the GraphQL useReportNotificationsSeenMutation mutation. Pass the previousCursor (the top cursor) of the notifications page you showed. After this, the timeline's unreadAfterSortIndex moves up to your newest notification and Twitter's own unread count clears on every device.
| Argument | Type | Description |
|---|---|---|
cursor | string | previousCursor from notifications.timeline() |
type | string | "All" (default), "Mentions" or "Verified" |
const page = await client.notifications.timeline();
await client.notifications.markSeen(page.previousCursor);notifications.devicePosts(opts?)
New posts from accounts you turned on post notifications for (the bell on a profile). This is what the "New post notifications for ..." entry opens on x.com. Uses GET /2/notifications/device_follow.json.
const { tweetIds, cursor } = await client.notifications.devicePosts();
const older = await client.notifications.devicePosts({ cursor });Returns { tweetIds, cursor, raw }. tweetIds are newest first and cursor pages older posts. Hydrate the ids with tweets.getMany or playPostsQuery if you want full post objects, since raw.globalObjects holds the older REST shape.
notifications.stream(opts?)
Receive Twitter's push notifications live, the same ones x.com shows as browser notifications. emusks subscribes through Mozilla autopush like Firefox does, registers the endpoint with v1.1 notifications/settings/login, checks in through notifications/settings/checkin when Twitter's checkin_time passes, and decrypts each push with its own keys.
| Option | Type | Description |
|---|---|---|
opts.state | object | stream.state from an earlier run, to keep the same keys |
opts.device | string | Device name Twitter shows for the registration (Mac/Chrome) |
opts.locale | string | Language of the notification text (en) |
opts.url | string | Autopush websocket url |
const stream = await client.notifications.stream();
for await (const n of stream) {
console.log(n.type, n.title, n.body, n.tweetId);
}You can also listen for events instead of iterating:
stream.on("notification", (n) => console.log(n.uri));
stream.on("reconnect", () => console.log("socket came back"));
stream.on("error", console.error);Each notification is { type, tweetId, userId, title, body, uri, timestamp, raw }. type is Twitter's scribe_target, and raw is the decrypted payload.
The stream reconnects with backoff when the socket drops, and registers again with Twitter if autopush hands out a new endpoint. Save stream.state and pass it back as opts.state to keep the same keys and registration across restarts. With a saved state.registration, a restart before checkin_time skips the login call.
stream.close() ends the socket and the iterator. stream.close({ logout: true }) also tells Twitter to stop pushing to this endpoint.
account.badgeCounts()
Get the unread counts x.com shows on the sidebar badges. Uses the GraphQL ViewerBadgeCounts query with the x-twitter-polling header, like the web app's poller.
const counts = await client.account.badgeCounts();
// { dm_unread_count: 0, ntab_unread_count: 1, xchat_unread_count: 0, total_unread_count: 1, is_from_urt: true }