# Generate/Regenerate api\_key for a user API not working

**URL:** <https://meta.discourse.org/t/generate-regenerate-api-key-for-a-user-api-not-working/135039>\
**Category:** Development\
**Tags:** rest-api\
**Created:** [December 4, 2019, 8:38am UTC](https://meta.discourse.org/t/generate-regenerate-api-key-for-a-user-api-not-working/135039 "2019-12-04T08:38:21Z")\
**Posts on this page:** 6\
**Page:** 1

## AI-generated summary

[NathanLei](/t/-/135039/1) reported a 404 error when trying to generate an API key for a user via the documented endpoint, despite other APIs working and backend setup being correct. [david](/t/-/135039/2) clarified that the endpoint has changed and provided the updated cURL command to create an API key using `POST /admin/api/keys`, while also questioning the use case. [NathanLei](/t/-/135039/3) explained they created a new user via API and wanted to post on behalf of that user, assuming they needed a dedicated API key for it. [david](/t/-/135039/4) offered a better solution: use a single "all users" API key and switch the `Api-Username` header per request to impersonate different users — referencing the docs for an example. [NathanLei](/t/-/135039/5) confirmed this approach works perfectly. The documentation is being updated to reflect the endpoint change.

<div class="post-metadata">

**Author:** ![NathanLei](https://avatars.discourse-cdn.com/v4/letter/n/6bbea6/32.png) [@NathanLei](https://meta.discourse.org/u/NathanLei)\
**Post date:** [December 4, 2019, 8:38am UTC](https://meta.discourse.org/t/generate-regenerate-api-key-for-a-user-api-not-working/135039/1 "2019-12-04T08:38:21Z")

</div>

I m trying to get an API key . for a user , but return me 404,

my other APIs work fine, so not the header issue. I also checked the backend setup. no problem..

> **[Discourse API Docs](https://docs.discourse.org/#tag/Admin%2Fpaths%2F~1admin~1users~1%7Bid%7D~1generate_api_key%2Fpost)**

---

<div class="post-metadata">

**Author:** ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)\
**Post date:** [December 4, 2019, 12:04pm UTC](https://meta.discourse.org/t/generate-regenerate-api-key-for-a-user-api-not-working/135039/2 "2019-12-04T12:04:26Z")

</div>

The endpoint to create a new API key has changed - we’re working on updating the documentation ASAP. You can now create a new API key like this:

```plaintext
curl -X POST http://localhost:3000/admin/api/keys \
  -H "Content-Type: multipart/form-data;" \
  -H "Api-Key: $api_key" \
  -H "Api-Username: system" \
  -F "key[description]=testing" \
  -F "key[username]=bobby"

```

I would be interested to know why you are creating new API keys… using an API key. It seems a little circular, and I wonder if we can come up with a better solution for your problem?

---

<div class="post-metadata">

**Author:** ![NathanLei](https://avatars.discourse-cdn.com/v4/letter/n/6bbea6/32.png) [@NathanLei](https://meta.discourse.org/u/NathanLei)\
**Post date:** [December 5, 2019, 3:26am UTC](https://meta.discourse.org/t/generate-regenerate-api-key-for-a-user-api-not-working/135039/3 "2019-12-05T03:26:08Z")

</div>

I have created a new user via API. (with admin API and KEY)  
now I need to post a post or topic for this user.

But seems no API for me to do that.. So I think maybe I should create a API for this user, then I switch my header with this new API… then I can make a post or topic on behalf of this new user..

---

<div class="post-metadata">

**Author:** ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)\
**Post date:** [December 5, 2019, 10:02am UTC](https://meta.discourse.org/t/generate-regenerate-api-key-for-a-user-api-not-working/135039/4 "2019-12-05T10:02:32Z")

</div>

Rather than creating a key for each user, you can use an “all users” key, then change the `Api-Username` header on the request. There is an example at the top of [https://docs.discourse.org/](https://docs.discourse.org/)

---

<div class="post-metadata">

**Author:** ![NathanLei](https://avatars.discourse-cdn.com/v4/letter/n/6bbea6/32.png) [@NathanLei](https://meta.discourse.org/u/NathanLei)\
**Post date:** [December 6, 2019, 8:13am UTC](https://meta.discourse.org/t/generate-regenerate-api-key-for-a-user-api-not-working/135039/5 "2019-12-06T08:13:35Z")

</div>

great man! thanks. that works perfectly !

---

<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:** [January 5, 2020, 8:18am UTC](https://meta.discourse.org/t/generate-regenerate-api-key-for-a-user-api-not-working/135039/6 "2020-01-05T08:18:09Z")

</div>

Closed on January 5, 2020, 8:18am UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
