Return the currently logged-in X/Twitter user (handle + name).
{}{"name":"visible name","userId":"own numeric id","screen_name":"handle (without @)"}Return the currently logged-in X/Twitter user (handle + name). Reads the user-id from the twid cookie, discovers the current UserByRestId queryId from the JS bundles (survives per-deploy rotation) and resolves screen_name+name via GraphQL UserByRestId with the public bearer + ct0 cookie (does NOT require the anti-bot signature x-client-transaction-id). No params. Returns { userId, screen_name, name }. Replaces the old x-whoami that used account/settings.json, now broken (404 without signature). Requires an X session.
params. It does the in-page work and returns the JSON in returns.( async () => { const scripts=[...document.querySelectorAll('script[src]')].map(s=>s.src).filter(u=>/abs.twimg.com/.test(u)); let queryId=null; for(const src of scripts){try{const t=await(await fetch(src)).text(); const m=t.match(/queryId:"([^"]+)",operationName:"UserByRestId"/); if(m){queryId=m[1];break;}}catch(e){}} if(!queryId) throw new Error('queryId UserByRestId not found'); const twid=document.cookie.split('; ').find(c=>c.startsWith('twid='))?.slice(5)||''; const userId=decodeURIComponent(twid).replace(/^u=/,''); if(!userId) throw new Error('no twid cookie: X session not started'); const ct0=document.cookie.split('; ').find(c=>c.startsWith('ct0='))?.slice(4)||''; const bearer='AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA'; const variables={userId,withSafetyModeUserFields:true}; const features={hidden_profile_subscriptions_enabled:true,profile_label_improvements_pcf_label_in_post_enabled:true,rweb_tipjar_consumption_enabled:false,responsive_web_graphql_exclude_directive_enabled:true,verified_phone_label_enabled:false,subscriptions_verification_info_is_identity_verified_enabled:true,subscriptions_verification_info_verified_since_enabled:true,highlights_tweets_tab_ui_enabled:true,responsive_web_twitter_article_notes_tab_enabled:true,subscriptions_feature_can_gift_premium:true,creator_subscriptions_tweet_preview_api_enabled:true,responsive_web_graphql_skip_user_profile_image_extensions_enabled:false,responsive_web_graphql_timeline_navigation_enabled:true}; const url=https://x.com/i/api/graphql/${queryId}/UserByRestId?variables=${encodeURIComponent(JSON.stringify(variables))}&features=${encodeURIComponent(JSON.stringify(features))}; const headers={authorization:'Bearer '+bearer,'x-csrf-token':ct0,'x-twitter-active-user':'yes','x-twitter-auth-type':'OAuth2Session','content-type':'application/json'}; const r=await fetch(url,{headers,credentials:'include'}); const j=await r.json(); const u=j?.data?.user?.result; if(!u) throw new Error('UserByRestId empty: '+JSON.stringify(j.errors||j)); const legacy=u.legacy||{}; return { userId, screen_name: legacy.screen_name||u?.core?.screen_name||null, name: legacy.name||u?.core?.name||null }; } )
{
"type": "json",
"jsonPath": "screen_name"
}