Skip to content

Commit f6a719b

Browse files
committed
Add error handling example to README
1 parent b08b135 commit f6a719b

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,29 @@ Let's print the title of the first result, but in a more Pythonic way:
5050

5151
The [SerpApi.com API Documentation](https://serpapi.com/search-api) contains a list of all the possible parameters that can be passed to the API.
5252

53+
### Error handling
54+
55+
Unsuccessful requests raise `serpapi.HTTPError` exception. The returned status code will reflect the sort of error that occurred, please refer to [Status and Error Codes Documentation](https://serpapi.com/api-status-and-error-codes) for more details.
56+
57+
```python
58+
import serpapi
59+
60+
client = serpapi.Client(api_key=os.getenv("API_KEY"))
61+
62+
try:
63+
results = client.search({
64+
'engine': 'google',
65+
'q': 'coffee',
66+
})
67+
except serpapi.HTTPError as e:
68+
if e.status_code == 401: # Invalid API key
69+
print(e.error) # "Invalid API key. Your API key should be here: https://serpapi.com/manage-api-key"
70+
elif e.status_code == 400: # Missing required parameter
71+
pass
72+
elif e.status_code == 429: # Exceeds the hourly throughput limit OR account run out of searches
73+
pass
74+
```
75+
5376
## Documentation
5477

5578
Documentation is [available on Read the Docs](https://serpapi-python.readthedocs.io/en/latest/).
@@ -339,7 +362,6 @@ results = client.search({
339362
```
340363
- API Documentation: [serpapi.com/images-results](https://serpapi.com/images-results)
341364

342-
343365
## License
344366

345367
MIT License.

0 commit comments

Comments
 (0)