Acrogenesis' Blog

Acrogenesis' Blog

Stories of a developer life.

Acrogenesis' Blog
< Back

How to bypass API restrictions


Don’t you hate it when a free API has limits on the number of request you can do? I was hitting the and API’s limits often. The problem was when we had a traffic spike. We didn’t want to pay for premium the same way we don’t want to pay extra bandwidth in case of a spontaneous traffic surge. So we came with the idea to cache it. Here’s an easy way to ‘cache’ the API’s JSON response.

First of all let’s create a shell script (.sh) (I’ll be doing the example for the wunderground API) and add some code (I use VIM, the vim command will create the file)

cd /home/user/pathto/weatherapi
wget -O

This script works as follows: It goes to the path where you created the  get’s the json from the API we are trying to bypass restrictions.

Now instead of calling on your website call this will give us the same file but locally bypassing the amounts of calls done to the api server.

Now we need to refresh the file we made locally so for this we make a cronjob, to make a cronjob type

crontab -e

This will open the cron editor, just add the following and we should be refreshing our local json every hour

0 * * * * /home/user/pathto/weatherapi/

The api call to the service will only be done every hour instead of every hit on your website. Now we are good to go!

Good luck! @acrogenesis

Thanks /u/Cixis for some suggestions