Skip to content

Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.

fix(x): update query IDs to resolve 422 GRAPHQL_VALIDATION_FAILED error

spacesdrive4042f2ba68df
diff --git a/backend/services/x.js b/backend/services/x.js
index 9de71f3..a915801 100644
--- a/backend/services/x.js
+++ b/backend/services/x.js
@@ -1,17 +1,19 @@
// X (Twitter) internal GraphQL API access via session cookies.
// Requires auth_token + ct0 from a logged-in x.com browser session.
// Bearer token and query IDs come from X's own web client JS bundles.
-// Query IDs rotate when X ships a frontend update - update X_QUERY_IDS when 400s appear.
+// Query IDs rotate when X ships a frontend update. Symptom: 400 or 422 GRAPHQL_VALIDATION_FAILED.
+// Get current IDs from https://github.com/vladkens/twscrape (twscrape/api.py OP_* constants).
// Bearer token is more stable but may also change - update X_BEARER if all requests return 401/403.
-// Last verified: 2026-01
+// Last verified: 2026-07
const X_BEARER = 'AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA';
const X_GQL_BASE = 'https://x.com/i/api/graphql';
-// GraphQL query IDs change when X rotates its web client. Update here if requests return 400.
+// GraphQL query IDs change when X rotates its web client. Update here if requests return 400/422.
+// Source: https://github.com/vladkens/twscrape/blob/main/twscrape/api.py
const X_QUERY_IDS = {
- UserByScreenName: '-oaLodhGbbnzJBACb1kk2Q',
- UserTweets: 'oPHs3ydu7ZOOy2f02soaPA',
+ UserByScreenName: '2qvSHpkWTMS9i0zJAwDNiA',
+ UserTweets: 'hr4gzZONlq23okjU8fIe_A',
};
// Minimal feature flags - required by X's GraphQL alongside variables.
@@ -75,8 +77,8 @@ function xHeaders(authToken, ct0) {
async function xFetch(url, authToken, ct0) {
const res = await fetch(url, { headers: xHeaders(authToken, ct0) });
- if (res.status === 400) {
- throw new Error('X GraphQL query ID is outdated. Update X_QUERY_IDS in backend/services/x.js - get current IDs from X\'s web client JS bundles or from github.com/she-llac/twitter-graphql-scraper');
+ if (res.status === 400 || res.status === 422) {
+ throw new Error('X GraphQL query ID is outdated. Update X_QUERY_IDS in backend/services/x.js - get current IDs from https://github.com/vladkens/twscrape (twscrape/api.py OP_UserByScreenName / OP_UserTweets constants)');
}
if (res.status === 401) {
// Try to read the body for more detail before throwing
diff --git a/docs/architecture/backend/SERVICES.md b/docs/architecture/backend/SERVICES.md
index 72b2a01..1d25e12 100644
--- a/docs/architecture/backend/SERVICES.md
+++ b/docs/architecture/backend/SERVICES.md
@@ -128,7 +128,7 @@ Reddit integration requires no Worker secrets. The session cookie is a per-user
### HTTP Client
-All X (Twitter) internal GraphQL API calls go through `xFetch(url, authToken, ct0)`. Requests include the X web client bearer token in `Authorization`, the `ct0` value in `x-csrf-token`, and both cookies in `Cookie`. The bearer token (`X_BEARER` constant) is X's own web client token and has been stable for years. GraphQL query IDs (`X_QUERY_IDS`) rotate when X deploys frontend changes and must be updated in the service file when HTTP 400 errors appear.
+All X (Twitter) internal GraphQL API calls go through `xFetch(url, authToken, ct0)`. Requests include the X web client bearer token in `Authorization`, the `ct0` value in `x-csrf-token`, and both cookies in `Cookie`. The bearer token (`X_BEARER` constant) is X's own web client token and changes infrequently. GraphQL query IDs (`X_QUERY_IDS`) rotate when X deploys frontend changes — symptoms are HTTP 400 or 422 `GRAPHQL_VALIDATION_FAILED`. Get current IDs from `OP_UserByScreenName` / `OP_UserTweets` in https://github.com/vladkens/twscrape (twscrape/api.py).
### Key Functions
diff --git a/docs/architecture/data-flows/ANALYTICS.md b/docs/architecture/data-flows/ANALYTICS.md
index 9bd1113..3aedf23 100644
--- a/docs/architecture/data-flows/ANALYTICS.md
+++ b/docs/architecture/data-flows/ANALYTICS.md
@@ -277,7 +277,7 @@ XAnalytics.jsx mounts -> api.getXAnalytics(id)
Note: `impressionCount` requires authentication as the account owner. It is available for own tweets via X's `views.count` field in the GraphQL response. Older tweets or tweets viewed without authentication return 0.
-Note: X's GraphQL `queryId` values in `X_QUERY_IDS` (backend/services/x.js) rotate when X deploys frontend updates. Update them when the API returns HTTP 400.
+Note: X's GraphQL `queryId` values in `X_QUERY_IDS` (backend/services/x.js) rotate when X deploys frontend updates. Symptoms are HTTP 400 or HTTP 422 with `GRAPHQL_VALIDATION_FAILED`. Get current IDs from https://github.com/vladkens/twscrape — look for `OP_UserByScreenName` and `OP_UserTweets` in `twscrape/api.py`.
## Overview Dashboard (Cross-Account)