# 500 Errors on Rate Limit When Using the Admin API Via Keys

**URL:** https://meta.discourse.org/t/500-errors-on-rate-limit-when-using-the-admin-api-via-keys/77779
**Category:** Support
**Created:** [January 11, 2018, 7:46pm UTC](https://meta.discourse.org/t/500-errors-on-rate-limit-when-using-the-admin-api-via-keys/77779 "2018-01-11T19:46:11Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Geoffrey\_Challen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/geoffrey_challen/32/119637_2.png) [@Geoffrey\_Challen](https://meta.discourse.org/u/Geoffrey_Challen)
#### Post date: [January 11, 2018, 7:46pm UTC](https://meta.discourse.org/t/500-errors-on-rate-limit-when-using-the-admin-api-via-keys/77779/1 "2018-01-11T19:46:11Z")

</div>

I use Discourse as a course forum, and have a bunch of scripts that I use to manage the installation through the admin API. I have an API key for an admin user and use that to perform various administrative tasks.

Today I’m trying to disable a bunch of users since we are transitioning between semesters. For each student that has left the class, I first log them out and then suspend them.

Unfortunately I’m having two problems:

- My requests are hitting some kind of internal rate limit, which it seems like I can’t control. It looks like the logic to bypass rate limiting for admins doesn’t work when you are using API keys.
- The offending requests are returning 500 errors rather than 429s.

Here’s a snippet of the relevant logs:

```plaintext
Started PUT "/admin/users/926/suspend?api_key=[FILTERED]&api_username=admin" for 192.17.148.54 at 2018-01-11 19:45:18 +0000
RateLimiter::LimitExceeded (RateLimiter::LimitExceeded)
/var/www/discourse/lib/rate_limiter.rb:87:in `performed!'

```

Ideally this rate limiting (a) wouldn’t be done for admin requests and (b) wouldn’t be returning the wrong error code.

---

<div class="post-metadata">

### Author: ![mikechristopher](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mikechristopher/32/89135_2.png) [@mikechristopher](https://meta.discourse.org/u/mikechristopher)
#### Post date: [January 11, 2018, 7:52pm UTC](https://meta.discourse.org/t/500-errors-on-rate-limit-when-using-the-admin-api-via-keys/77779/2 "2018-01-11T19:52:00Z")

</div>

I thought the rate limiter had been disabled for staff users so this might be one for the team to answer.

```
 def rate_unlimited?
!!(RateLimiter.disabled? || (@user && @user.staff?))
  end

```

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [January 11, 2018, 8:04pm UTC](https://meta.discourse.org/t/500-errors-on-rate-limit-when-using-the-admin-api-via-keys/77779/3 "2018-01-11T20:04:07Z")

</div>

> [@Geoffrey\_Challen](#):
>
> The offending requests are returning 500 errors rather than 429s.

Any ideas on this @sam?

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [January 11, 2018, 8:09pm UTC](https://meta.discourse.org/t/500-errors-on-rate-limit-when-using-the-admin-api-via-keys/77779/4 "2018-01-11T20:09:37Z")

</div>

Are we hosting your site or are you self hosted? What version are you running?

---

<div class="post-metadata">

### Author: ![Geoffrey\_Challen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/geoffrey_challen/32/119637_2.png) [@Geoffrey\_Challen](https://meta.discourse.org/u/Geoffrey_Challen)
#### Post date: [January 11, 2018, 8:10pm UTC](https://meta.discourse.org/t/500-errors-on-rate-limit-when-using-the-admin-api-via-keys/77779/5 "2018-01-11T20:10:15Z")

</div>

I know—I saw this. But it doesn’t seem to be working. Hence my speculation about the API key not setting up the user properly. But that’s just a guess.

---

<div class="post-metadata">

### Author: ![Geoffrey\_Challen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/geoffrey_challen/32/119637_2.png) [@Geoffrey\_Challen](https://meta.discourse.org/u/Geoffrey_Challen)
#### Post date: [January 11, 2018, 8:11pm UTC](https://meta.discourse.org/t/500-errors-on-rate-limit-when-using-the-admin-api-via-keys/77779/6 "2018-01-11T20:11:12Z")

</div>

Self hosted. On tests-passed, currently v2.0.0.beta1+76.

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [January 12, 2018, 3:28am UTC](https://meta.discourse.org/t/500-errors-on-rate-limit-when-using-the-admin-api-via-keys/77779/7 "2018-01-12T03:28:34Z")

</div>

OK this is fixed per:

> [@We should never leak out exceptions in our controller tests](https://meta.discourse.org/t/we-should-never-leak-out-exceptions-in-our-controller-tests/77800):
>
> I just finished pushing a rather gnarly change per: It basically replaces this pattern we used previously in tests: expect do get :index, format: :json end.to raise\_error(Discourse::NotLoggedIn) With this get :index, format: :json expect(response.status).to eq(403) I made this change so our test environment is much more inline with our production environment. Previously we were leaking out a few exceptions and erroneously returning 500 status when we had a far more appropriate status…

But it will take a bit for it to land cause I need to clean plugin tests.

The particular reason you are getting rate limited is:

> <https://github.com/discourse/discourse/blob/main/config/discourse_defaults.conf#L177>

if you are self hosting you can raise `DISCOURSE_MAX_ADMIN_API_REQS_PER_KEY_PER_MINUTE` to a higher number. We introduced this limit to protect from unintentional API abuse.

---

<div class="post-metadata">

### Author: ![Geoffrey\_Challen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/geoffrey_challen/32/119637_2.png) [@Geoffrey\_Challen](https://meta.discourse.org/u/Geoffrey_Challen)
#### Post date: [January 12, 2018, 2:00pm UTC](https://meta.discourse.org/t/500-errors-on-rate-limit-when-using-the-admin-api-via-keys/77779/8 "2018-01-12T14:00:41Z")

</div>

OK done, thanks! We’ll see if this helps.

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [March 6, 2023, 9:35am UTC](https://meta.discourse.org/t/500-errors-on-rate-limit-when-using-the-admin-api-via-keys/77779/11 "2023-03-06T09:35:12Z")

</div>



---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [March 7, 2023, 6:54am UTC](https://meta.discourse.org/t/500-errors-on-rate-limit-when-using-the-admin-api-via-keys/77779/12 "2023-03-07T06:54:51Z")

</div>


