Skip to content
TORNLIFE More

API Fix

Started by nepherius [2009878] on in API Development.

7 replies · 283 views · thread synced · 7 days ago · View on torn.com
About this thread

Posts archived: 8 / 8 posts (100%) · the total is Torn's reply count + the opening post at the last fetch

Counted by TornLife from the archived posts.

Archived posts
8
Discussion span
→
People posting
4
Likes on archived posts
5
Authority score
67 / 100
Historical score
20 / 100
Story score
31 / 100
Engagement score
55 / 100
nepherius [2009878]

For those having issues with the  torn API, getting CORS policy errors (503)

If you have a chrome/firefox extension add https://api.torn.com to the manifest in permissions array

For tampermonkey use GM_xmlhttpRequest instead of a regular xmlhttpRequest

These 2 lines are very important:
// @connect api.torn.com
// @grant GM_xmlhttpRequest
 
 
function getItems(api_key) {
  return new Promise((resolve, reject) => {
     let ret = GM_xmlhttpRequest({
       method: "GET",
       url: `https://api.torn.com/torn/?selections=items&key=${api_key}`,
       onreadystatechange: function (res) {
          if (res.readyState === 4 && res.status === 200) {
             const parsedRes = JSON.parse(res.responseText)
             Items = parsedRes.items
             resolve()
           }
        },
        onerror: function (err) {
           console.log(err)
        }
    })
 }).catch(e => {
    console.log(e)
 });
}



More info here https://github.com/scriptish/scriptish/wiki/GM_xmlhttpRequest

 

 

 

 

 
Afwas [1337627]

Failed to load resource: the server responded with a status of 503 ()


forums.php:1 Access to XMLHttpRequest at 'https://api.torn.com/torn/?selections=stocks&key=[...]&_=1548671466740' from origin 'https://www.torn.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Baldr [1847600] Reporter

I have a program, an executable that runs from my PC, that gathers item market info, current Value prices, that sort of thing. 

I normally run it once a day and it helps me price flowers/plushies and easily view Torn's Value amounts in my trade pricing tool.

But I get the 503.  Anything I can do to make that work?

I'm just pulling https://api.torn.com/torn/?selections=items&key= (plus my API key)

 
nepherius [2009878]

Depends on the program, you need to find some way to bypass the cloudflare protection. If by any chance you use nodejs, pm and I'll point you in the right direction :)
Baldr [1847600] Reporter

I dunno what nodejs is.  I'm not working with javascript, I'm not going through a browser.  Everything worked fine before, but now I just get the 503.

 
UwU [2081212]

Careful. You may end up breaching the TOS if you're making any non api requests such as loading up a torn page automatically with a browser or console http call which is explicitly disallowed.
nepherius [2009878]

Nothing is bypassed, the 5-second delay and redirect still happen.

 

Also ... they should have a special page rule for the API. I use Cloudflare on my site too, have a page rule for the API. Seems like lazy configuring to have the API follow the same rules as the regular page.