Search API returns 403 even though the rate limit isn't reached

Hi All,

I run the following bash script.

for i in `seq 1 10`
do
    curl -i "https://api.github.com/search/code?q=sample" \
         -H "Authorization: token OAUTH-TOKEN" \
         --verbose -o /dev/null \
         -s
done

Then I got the following response.

--- 1 ------------------
* Trying 192.30.255.117...
* TCP_NODELAY set
* Connected to api.github.com (192.30.255.117) port 443 (#0)

(ellipsis)

< HTTP/1.1 200 OK
< Date: Sun, 30 Sep 2018 23:44:16 GMT
< Content-Type: application/json; charset=utf-8
< Content-Length: 172333
< Server: GitHub.com
< Status: 200 OK
< X-RateLimit-Limit: 30
< X-RateLimit-Remaining: 29

--- 2 ------------------

(ellipsis)

< HTTP/1.1 200 OK
< Date: Sun, 30 Sep 2018 23:44:19 GMT
< Content-Type: application/json; charset=utf-8
< Content-Length: 172333
< Server: GitHub.com
< Status: 200 OK
< X-RateLimit-Limit: 30
< X-RateLimit-Remaining: 28

(ellipsis)

--- 4 ------------------

(ellipsis)

< HTTP/1.1 200 OK
< Date: Sun, 30 Sep 2018 23:44:23 GMT
< Content-Type: application/json; charset=utf-8
< Content-Length: 172333
< Server: GitHub.com
< Status: 200 OK
< X-RateLimit-Limit: 30
< X-RateLimit-Remaining: 26

(ellipsis)

--- 5 ------------------

(ellipsis)
< HTTP/1.1 403 Forbidden
< Date: Sun, 30 Sep 2018 23:23:27 GMT
< Content-Type: application/json; charset=utf-8
< Transfer-Encoding: chunked
< Server: GitHub.com
< Status: 403 Forbidden
< Retry-After: 60

It is strange that the fifth call fails although there are still 26 times remaining. Also, my IP address isn’t used by others to call Github API. Could someone please tell me the reason?

1 Like

Hi @kyoshidajp,

My apologies for taking so long to get back to you and thank you for reaching out. The rate limit allows for 30 calls per minute, what we are talking about is not the maximum number of requests you’re permitted to make per one minute, but the rate at which you can make calls over any period of time, there must be an average of 2 seconds between each request. I hope this helps!

Best,

Andrea

Hello,

I found this response in trying to understand the search API rate limit. In my program, I use the search_code method from the search API one time and often see the remaining requets number from get_rate_limit().search jump down from 30 to somewhere around 22/23 after running the program once then another jump down to 14/15 after running it again. I can’t understand why it’s not decrementing by one every time the program is run since there’s only one API call. How is the remaining requests number being calculated since the 30 requests per minute means an average of 2 seconds between each request? Furthermore, I inserted a line of code to sleep the program for 2 seconds right before the search_code call to force there to be 2 seconds in between each request, but I am still hitting the rate limit error occasionally. Any suggestions or explanations to help better my understanding of what is happening would be helpful. Thanks.