External API with rate limit / global state

I want to implement some functionality of the Twitch API. i do have it working in my typescript server code with simple http calls. BUT the api does have rate limits, which any client should account for.

I know that the rate limits are 800ish per minute, but i do expect to exceed these values eventually.

I was thinking the following: The server runtime should keep track of “Limit”, “Remaining” and “Reset” values that the api returns. But for this there needs to be some kind of global state.

I could maybe write some go code as per documentation it is possible but disencouraged, but i am myself not very comfortable with go.

Does anyone maybe have implemented some state regarding an external api or some ideas about that?

Hi @dela.

What do you hope to achieve by keeping track of the rate limit? Would simply checking the HTTP response for errors and handling it when it occurs not suffice? (i.e. retrying later or letting the user know the request failed)

What would you do in a scenario where you needed to perform the API request but you knew it wouldn’t succeed given the current state? e.g. if you knew your request wouldn’t be allowed for another 45 seconds. In that case I would imagine you would simply let the user know the action failed, which would be the same as if you made the Twitch API call and it told you itself that it was unsuccessful?

one could do it like that, yes.

i actually intended to read “Remaining” and always have “some requests left”, because some functionality of the api (auth token exchange) are more critical then other (e.g. sub-benefits checking)

If you decide you do need to store this information I would recommend using the storage engine to do this.