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 ✌🏾

--

--

--

I talk about real world experiences in Tech and Scaling Deep Learning based workloads | Reach out via @arjun921 / connect@arjunsunil.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Gridsome CMS — Netlify and Github repo — very short guide

netlify, gridsome and github logos

SOLIDITY ⛓ —  How to work with interfaces and payable ⚡️

Play with Images….

from Buffer

Become a Senior Developer in 2 Years

Champion/Challenger Experiment Design in Machine Learning Models

DEPOCKET — FAQs ABOUT US

Testing in Production (TiP), an effective strategy to prevent IT disasters

Get the Medium app

Arjun Sunil

Arjun Sunil

I talk about real world experiences in Tech and Scaling Deep Learning based workloads | Reach out via @arjun921 / connect@arjunsunil.com

More from Medium

NFT, OpenSea and Bot : Tokyo Sugar Baby Story 1 of 3

The A-Z (and the N, F, T) of NFTs

Ethernaut Solutions

NRC721— Deploy your NFTs in minutes