Skip to main content
 
 
 
IN THIS SECTION
3 posts
travisbarton
Last seen: 07/29/2022 - 18:40
Joined: 07/29/2022 - 13:45
bad endpoint for patent country

Hey all,

 

I'm running this test:

```

import requests

import pandas as pd

url = 'https://api.patentsview.org/patents/query'

r = requests.post(url, json={ "q": {"_and": [{'_text_any': {"patent_title": "renal"}}, {'_text_any': {"patent_title": "cell"}}, {'_contains': {'cpc_subsection_id': 'A61'}}, {'_gte': {'patent_date': '2015-01-01'}}] }, "f": ["patent_title", "patent_date", "patent_country"], "o": {"page": 1, "per_page": 100}})

print(r.json())

la = pd.json_normalize(r.json()['patents'])

```

 

and I get the error: `{'status': 'error', 'payload': {'error': 'Invalid field specified: patent_country ', 'code': 'PINV8'}}`

 

Am I using this endpoint wrong? or is it not functioning properly?

Russ
Last seen: 08/11/2022 - 14:20
Joined: 11/14/2017 - 22:15
slight confusion

Ah, I see your confusion.  The url you are using is the legacy / original version of the api.  patent_country is a field in only the beta version of the api.  You'll need to request an api key in order to use the new beta endpoints.  

You'll have to send in your api key on all requests to the beta endpoints.  You could do something like

 api_key = os.getenv('PATENTSVIEW_API_KEY')
 headers = {
    'X-Api-Key': api_key,
    'User-Agent': 'my-awesome-py-app'
 }
 r = requests.post(url, headers=headers, json=params)
 

You could get throttled by the new endpoint if you send in more that 45 requests per minute.  You could add this code

   # sleep then retry on a 429 Too many requests
   if 429 == r.status_code:
       print("Throttled response from the api, retry in {} seconds".format(r.headers["Retry-After"]))
       time.sleep(int(r.headers["Retry-After"]))  # Number of seconds to wait before sending next request
       r = requests.post(url, headers=headers, json=params)
 

Not sayin' I'm pythonic or anything
Russ