How to fix the Damn Opensea 403 Error in Python

Photo by Taylor Vick on Unsplash

We got rate limited. In production. On the day of our sale.

So, I was working on an NFT project that uses the Opensea API for a few operations, and few hours into going live; our backend stopped working. The API just wasn’t getting a successful response from Opensea.

Opensea API reliability was one of our fears in the back of our mind considering the scale at which our application was going to run.

But given the timeframe in which we had to go live, we just couldn’t spend enough time working on alternatives.

Okay, enough backstory. On to the fix

HOW DO I FIX THE DAMN THING?

This is what my snippet of code looked like that was trying to make the API request.

import requests
url = "https://api.opensea.io/api/v1/assets"
headers = {"X-Api-Key": config.get('OPENSEA_API_KEY')}
params = {'owner' : wallet, 'offset': offset, 'limit': limit}
response = requests.get(url, params=params, headers=headers)
print(response)
if response.status_code == 200:
return response.json()

This is what worked for me at scale.

import requests
url = "https://api.opensea.io/api/v1/assets"
# use these headers
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36','referrer':url, "X-Api-Key": config["OPENSEA_API_KEY"]}
params = {'owner' : wallet, 'offset': offset, 'limit': limit}
response = requests.get(url, params=params, headers=headers)
print(response)
if response.status_code == 200:
return response.json()

All it needed was the damn User-Agent and referrer in the header!

headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36','referrer':url, "X-Api-Key": config["OPENSEA_API_KEY"]}

Whats sad is no one has actually published about this fix.

One good reference for changing User Agents would be in the following link:
(Although beware, I haven’t tested out how does randomising affect the actual rate limiting in production)

One clap, two clap, three clap, forty?

If you found this useful, you know what to do now. Hit that clap button and follow me for more articles and tutorials on your feed.

Peace ✌🏾

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store