# 404 Error returned when using DiscourseAPI Gem to Generate User API Key

**URL:** https://meta.discourse.org/t/404-error-returned-when-using-discourseapi-gem-to-generate-user-api-key/140892
**Category:** Development
**Tags:** rest-api
**Created:** [February 6, 2020, 5:04pm UTC](https://meta.discourse.org/t/404-error-returned-when-using-discourseapi-gem-to-generate-user-api-key/140892 "2020-02-06T17:04:24Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Anthony\_Dirago](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/anthony_dirago/32/137283_2.png) [@Anthony\_Dirago](https://meta.discourse.org/u/Anthony_Dirago)
#### Post date: [February 6, 2020, 5:04pm UTC](https://meta.discourse.org/t/404-error-returned-when-using-discourseapi-gem-to-generate-user-api-key/140892/1 "2020-02-06T17:04:24Z")

</div>

Hi there,

I’ve been struggling to find a solution to this and I’m hoping someone here has some insight. Recently I joined a new project and took over for an old developer. I’ve recently upgraded the Discourse build to the latest version, it was a few months behind, and every other feature and api call is working as expected.

What I’m trying to do is use the API to create a topic in discourse from our website outside of discourse. I take the user’s ID and find them in discourse using the DiscourseAPI by\_external\_id method. Once I have that I call generate\_user\_api\_key(discourse\_user[‘id’]) which passes the ID for the user on discourse.

This responds with a DiscourseApi::NotFoundError / 404 error which only started after upgrading.

Any help or guidance on this is appreciated!

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [February 7, 2020, 12:28am UTC](https://meta.discourse.org/t/404-error-returned-when-using-discourseapi-gem-to-generate-user-api-key/140892/2 "2020-02-07T00:28:22Z")

</div>

> [@Anthony\_Dirago](#):
>
> This responds with a DiscourseApi::NotFoundError / 404 error which only started after upgrading.

The route for generating API keys has changed, but the method in the Discourse API gem that’s used for generating an API key is still calling the old route. That’s why you are getting the `404` error.

We will get this fixed. For now, if you would like to generate an API key for a user, you can do it by making a `POST` request to `http://localhost:3000/admin/api/keys"`. Here’s an example CURL request. I’ve substituted my actual API key with `$api_key` in the example:

```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]=a key for bobby" \
-F "key[username]=bobby"

```

> [@Anthony\_Dirago](#):
>
> What I’m trying to do is use the API to create a topic in discourse from our website outside of discourse.

You do not need to generate a user API key for this. Instead, you can set `client.api_username` to the username you want to use to publish the post:

```ruby
client.api_username = "susan"
client.create_topic(category: 5, title: "Susan's really awesome API topic", skip_validation: true, raw: "this is a test, this is only a test")

client.api_username = "bobby"
client.create_topic(category: 5, title: "Bobby's really awesome API topic", skip_validation: true, raw: "this is a test, this is only a test")

```

This will work correctly as long as you used the All Users API key when you created the `client`.

---

<div class="post-metadata">

### Author: ![Anthony\_Dirago](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/anthony_dirago/32/137283_2.png) [@Anthony\_Dirago](https://meta.discourse.org/u/Anthony_Dirago)
#### Post date: [February 7, 2020, 3:30pm UTC](https://meta.discourse.org/t/404-error-returned-when-using-discourseapi-gem-to-generate-user-api-key/140892/3 "2020-02-07T15:30:14Z")

</div>

Thank you!

I updated my code to use the “All Users” key instead of generating one for each user and pass the user’s username as the api\_username and that fixed it.

---

<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 8, 2020, 3:43pm UTC](https://meta.discourse.org/t/404-error-returned-when-using-discourseapi-gem-to-generate-user-api-key/140892/4 "2020-03-08T15:43:52Z")

</div>

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