# Specify user by external id

**URL:** https://meta.discourse.org/t/specify-user-by-external-id/87584
**Category:** Support
**Created:** [5월 16, 2018, 2:56오전 UTC](https://meta.discourse.org/t/specify-user-by-external-id/87584 "2018-05-16T02:56:45Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![mattman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mattman/32/119713_2.png) [@mattman](https://meta.discourse.org/u/mattman)
#### Post date: [5월 16, 2018, 2:56오전 UTC](https://meta.discourse.org/t/specify-user-by-external-id/87584/1 "2018-05-16T02:56:45Z")

</div>

I’m just starting to learn how to use the Discourse API.

I’ve got data coming back when I send an api\_key and api\_username to an endpoint.

However, specifying the user by their username seems mysterious and brittle. I need to know the right username but the user can change their username and I don’t know what it actually is from outside.

My Discourse users can only import an account from my Bubble app. In the SSO section there’s an ‘external id’ that looks like it’s the account’s unique id from Bubble.

**Is there a way to specify the user I want based on their external id instead of their username?**

---

<div class="post-metadata">

### Author: ![mpalmer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mpalmer/32/45740_2.png) [@mpalmer](https://meta.discourse.org/u/mpalmer)
#### Post date: [5월 16, 2018, 3:17오전 UTC](https://meta.discourse.org/t/specify-user-by-external-id/87584/2 "2018-05-16T03:17:01Z")

</div>

[`/users/by-external/<id>`](https://github.com/discourse/discourse/blob/master/config/routes.rb#L417).

---

<div class="post-metadata">

### Author: ![mattman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mattman/32/119713_2.png) [@mattman](https://meta.discourse.org/u/mattman)
#### Post date: [5월 16, 2018, 3:24오전 UTC](https://meta.discourse.org/t/specify-user-by-external-id/87584/3 "2018-05-16T03:24:46Z")

</div>

It looks like that’s returning a webpage? At least the DOCTYPE is html

---

<div class="post-metadata">

### Author: ![blake](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/blake/32/157322_2.png) [@blake](https://meta.discourse.org/u/blake)
#### Post date: [5월 16, 2018, 3:34오전 UTC](https://meta.discourse.org/t/specify-user-by-external-id/87584/4 "2018-05-16T03:34:55Z")

</div>

can you append `.json` to the end?

> [@mpalmer](#):
>
> [`/users/by-external/<id>`](https://github.com/discourse/discourse/blob/master/config/routes.rb#L417).json

---

<div class="post-metadata">

### Author: ![blake](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/blake/32/157322_2.png) [@blake](https://meta.discourse.org/u/blake)
#### Post date: [5월 16, 2018, 3:43오전 UTC](https://meta.discourse.org/t/specify-user-by-external-id/87584/5 "2018-05-16T03:43:18Z")

</div>

> [@mattman](#):
>
> Is there a way to specify the user I want based on their external id instead of their username?

So when using the api you can search for them by external\_id, but you still need to reference their username in most api calls, like in order to get their notifications.

You would call this endpoint, and specify their username:

```
https://meta.discourse.org/notifications.json?username=<username>&api_key=<api_key>&api_username=system

```

---

<div class="post-metadata">

### Author: ![mattman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mattman/32/119713_2.png) [@mattman](https://meta.discourse.org/u/mattman)
#### Post date: [5월 16, 2018, 3:50오전 UTC](https://meta.discourse.org/t/specify-user-by-external-id/87584/6 "2018-05-16T03:50:27Z")

</div>

Ah, okay, yes that returns json. It looks like a user’s profile information. I see the current username under user \> username.

So do I have to lookup the current username based on the external id and then use the confirmed username in a second request?

Like, is there a way to use these parameters to get info in one request?  
api\_key  
api\_username  
external\_id (instead of username)

It seems weird to base requests on a field that can change instead of some system generated id field that never changes.

---

<div class="post-metadata">

### Author: ![blake](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/blake/32/157322_2.png) [@blake](https://meta.discourse.org/u/blake)
#### Post date: [5월 16, 2018, 4:03오전 UTC](https://meta.discourse.org/t/specify-user-by-external-id/87584/7 "2018-05-16T04:03:01Z")

</div>

> [@mattman](#):
>
> So do I have to lookup the current username based on the external id and then use the confirmed username in a second request?

Correct, but you can cache it depending on what you are doing, so you don’t have to look it up everytime.

> [@mattman](#):
>
> is there a way to use these parameters to get info in one request?

Nope.

> [@mattman](#):
>
> It seems weird to base requests on a field that can change instead of some system generated id field that never changes.

Do your usernames change a lot though? I do kind of agree with you, but even if it was by `id` you would still have to look up the users `id` based on the `external_id` and use that.

I’m not totally sure though but some endpoints might take either one (username or id), I know this isn’t a user endpoint, but for example the topic endpoint you can do something like this and leave out the slug:

```
https://meta.discourse.org/t/-/87383.json

```

I digress though, since most endpoints do key off of the username.

---

<div class="post-metadata">

### Author: ![mattman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mattman/32/119713_2.png) [@mattman](https://meta.discourse.org/u/mattman)
#### Post date: [5월 16, 2018, 4:05오전 UTC](https://meta.discourse.org/t/specify-user-by-external-id/87584/8 "2018-05-16T04:05:48Z")

</div>

Thanks. I think I can work with that.

Is there a way to get the current number of unread notifications? Like the number that displays in Discourse? I want to display that number back in the Bubble app so users know to check the forum.

It looks like what’s returned from notifications.json is a list of notifications?

Does Discourse have an immutable id for each account? I guess I just assumed it would since that’s how everything I’m familiar with works.

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [5월 16, 2018, 4:17오전 UTC](https://meta.discourse.org/t/specify-user-by-external-id/87584/9 "2018-05-16T04:17:48Z")

</div>

You should only need varying api\_username when you’re acting “as” varying users.

You should be able to pull notifications for other users by loading `/notifications.json?silent=true&username=abcd&offset=0` from an admin account.

(note: silent=true prevents your automated code from marking everyone’s notifications as read/seen)

---

<div class="post-metadata">

### Author: ![mattman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mattman/32/119713_2.png) [@mattman](https://meta.discourse.org/u/mattman)
#### Post date: [5월 17, 2018, 4:41오전 UTC](https://meta.discourse.org/t/specify-user-by-external-id/87584/10 "2018-05-17T04:41:55Z")

</div>

It makes sense that api\_key and api\_username are for authentication and then “username” is to identify the user I’m interested in.

Is there a way to get the total “unread” notifications?  
Does that have to be calculated from the list of notifications and the seen\_notification\_id?

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [5월 17, 2018, 5:04오전 UTC](https://meta.discourse.org/t/specify-user-by-external-id/87584/11 "2018-05-17T05:04:55Z")

</div>

To get the blue/green numbers, load `/session/current.json` as the desired user.

`,"unread_notifications":0,"unread_private_messages":0,`

---

<div class="post-metadata">

### Author: ![mattman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mattman/32/119713_2.png) [@mattman](https://meta.discourse.org/u/mattman)
#### Post date: [5월 17, 2018, 5:09오전 UTC](https://meta.discourse.org/t/specify-user-by-external-id/87584/12 "2018-05-17T05:09:10Z")

</div>

Oh, cool! Thanks.  
Okay, so it looks like that endpoint is only giving me my session info. The info for the username in api\_username. When I add another “username” parameter it seems to be ignored.  
This sounds similar [Session/current always returns api\_user](https://meta.discourse.org/t/session-current-always-returns-api-user/72875)

Is there a better way to discover this stuff than bothering people in the forum?  
[docs.discourse.org](http://docs.discourse.org) looks pretty sparse, like I can’t even find that “session” endpoint when I search for it specifically.

---

<div class="post-metadata">

### Author: ![blake](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/blake/32/157322_2.png) [@blake](https://meta.discourse.org/u/blake)
#### Post date: [5월 17, 2018, 9:06오후 UTC](https://meta.discourse.org/t/specify-user-by-external-id/87584/13 "2018-05-17T21:06:45Z")

</div>

> [@mattman](#):
>
> Is there a better way to discover this stuff than bothering people in the forum?

Please checkout this topic:

> [@Reverse engineer the Discourse API](https://meta.discourse.org/t/how-to-reverse-engineer-the-discourse-api/20576):
>
> Discourse is backed by a complete JSON api. Anything you can do on the site you can also do using the JSON api. The API is documented at [docs.discourse.org](https://docs.discourse.org). You can also use the [discourse\_api](https://github.com/discourse/discourse_api) Ruby gem as a client library. However, not every endpoint is documented. To determine how to do something with the JSON API here are some steps you can follow. Example: recategorize a topic. Go to a topic and start editing a category: Open Chrome dev tools, switch to the Network tab, select …

and also looking at the source code for the controller of the endpoint you are trying to hit is very helpful:

> <https://github.com/discourse/discourse/blob/master/app/controllers/session_controller.rb#L321-L327>

> [@mattman](#):
>
> When I add another “username” parameter it seems to be ignored.

This is because that endpoint doesn’t have support for a “username” param.

> [@mattman](#):
>
> [docs.discourse.org](http://docs.discourse.org) looks pretty sparse, like I can’t even find that “session” endpoint when I search for it specifically.

😢

The [api docs](https://github.com/discourse/discourse_api_docs) ~~could use~~ will always need some work, but they do document most of the heavier used endpoints. If you find a new one we would very much appreciate the help of the community to help document it. I will keep trucking along myself though and continue to document things at a 🐢 steady pace as time permits.

 ![image](https://global.discourse-cdn.com/meta/original/3X/a/8/a8c4b8ad20837446b133f1b74a76ac2c96fe1c9d.jpg)

> [@mattman](#):
>
> from my Bubble app

I’m not familiar with Bubble. Is this what you are referring to?

[https://bubble.is/](https://bubble.is/)

Either way, Discourse by nature has an api endpoint for most things, but that doesn’t necessarily mean that it will always be super friendly when you are trying to call things from external sites instead of natively from inside Ember with a logged in user cookie. This may mean that it will be more productive to [write a custom plugin](https://meta.discourse.org/t/beginners-guide-to-creating-discourse-plugins-part-1/30515) or use the [data explorer plugin](https://meta.discourse.org/t/data-explorer-plugin/32566) to write a custom query and then make an api request to that saved query. Also PR’s are always welcome to improve something.

---

<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: [11월 4, 2020, 11:23오전 UTC](https://meta.discourse.org/t/specify-user-by-external-id/87584/14 "2020-11-04T11:23:57Z")

</div>



---

<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: [11월 5, 2020, 1:38오전 UTC](https://meta.discourse.org/t/specify-user-by-external-id/87584/15 "2020-11-05T01:38:33Z")

</div>


