How to create an api key on the admin panel

I just got Discourse setup on Digital Ocean and I’m trying to tie it into my app on Bubble.

I found the API docs https://docs.discourse.org/ which say that I need to create an API key on the admin panel.


However, I don’t see any way to create a new API key.

I tried using that one API key in a parameter called “api_key” on a test call and the message said it wasn’t accepted.

So I’d like to ask about how to get the user’s notification count but I can’t even get authenticated yet.

I’d be happy to educate myself but I can’t find any documentation or tutorials on how to get started calling the Discourse API.

2 Likes

You cannot create more than one api key, any reason why you can’t create more than one api key @codinghorror @sam.

You can create one per user at the moment, not against changing it to allow multiple per user but there is a simple workaround (of creating api user per api key)

You can create as many user api keys as you want though per user.

5 Likes

Okay. So what do the API docs mean when they say “To become authenticated you will need to create an API Key from the admin panel”? That’s, like, the first step in the documentation.

What it means is that when you start with a brand new install of Discourse you should not have an api key and when you go to the API tab it should look like this:

So you will need to click that key icon to create a system wide api key. I’ll work on improving the docs about how to create a system key and how to make user api keys.

That key most definitely should work, most likely something else was the issue and you might need to provide more info with how you made the api request so that we can help you troubleshoot. Also please check if the content-type was set to form-data.

You will need to hit the /notifications.json endpoint and pass in the user you want notifications for in the api_username field.

2 Likes

Oh, okay. Discourse was installed and setup for me so I didn’t see the step where the admin’s API key was generated.

I switched from application/json to form-data and got a different error, but it looks like I made it past the authentication step.

If I just included an api_key then I got an error that either the key or username was wrong, which makes sense. If I include an api_key and an api_username then I get an error 422 “unprocessable entity” or an error 400 “invalid access” depending on what I put in the api_username field.

What “username” am I supposed to use to identify an account? The user’s profile has two fields called “username” and the only one that’s populated is something that can be edited by the user.

I’m only allowing users to make a Discourse account by using SSO to pull in the account they already made on the Bubble app. There’s an “external ID” that seems to be the user’s unique ID from Bubble.

I tried replacing the api_username field with external_id but got the same “key or username is invalid” error.

You want the 422 - that means there’s some other problem with the data you’re sending, other than the authentication.

I figured.
But there’s only two fields.
I’m confident the api_key is correct, otherwise it would refuse access every time.
So there’s something about the api_username field that is passing authentication but is somehow invalid after that.

It’s not even clear what I’m supposed to put in the api_username field, as I described in the previous post.

If you created an “all users” API key should the name be “system”?

https://meta.discourse.org/t/get-user-data-remotely-api-database-curl/31415/4?u=mittineague

1 Like

Putting “system” in the api_username field does get me 422 “unprocessable entity” again.

I could be off base, but “entity” reminds me of invalid URLs. eg. instead of &param=something it interprets &param as an invalid entity. Maybe if you urlencode the ampersand?

Hokay, so, I guess technically this topic is solved since the API key on the admin panel thing is resolved.