# Request from API doesn't update User#last\_seen\_at

**URL:** https://meta.discourse.org/t/request-from-api-doesnt-update-user-last-seen-at/108528
**Category:** Support
**Created:** [February 6, 2019, 7:56pm UTC](https://meta.discourse.org/t/request-from-api-doesnt-update-user-last-seen-at/108528 "2019-02-06T19:56:10Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![jandres](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jandres/32/130933_2.png) [@jandres](https://meta.discourse.org/u/jandres)
#### Post date: [February 6, 2019, 7:56pm UTC](https://meta.discourse.org/t/request-from-api-doesnt-update-user-last-seen-at/108528/1 "2019-02-06T19:56:11Z")

</div>

Last version is ignoring request coming from API to update user visits stats in forum.

Other stats like ‘number of likes’ or ‘pageviews’ are right, but it seems that something has happened with user visit stats.

Before, when i pulled for latest topics or post in topics, it updated the User#last\_seen\_at dates, but now there are now 9 days ago, I don’t have any new visit from my users, when normally i have hundreds.

When i visit the user active list page in ‘/admin/users/list/active’, i see the ‘seen’ column fixed to date when i did the update.

I updated from 2.2.0.beta3 to last current version 2.3.0.beta1 and i didn’t have any issue while updating.

Any help with this issue, is really appreciated

---

<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: [February 6, 2019, 8:13pm UTC](https://meta.discourse.org/t/request-from-api-doesnt-update-user-last-seen-at/108528/2 "2019-02-06T20:13:46Z")

</div>

This is expected, “last seen” should not be updated on API requests.

> <https://github.com/discourse/discourse/blob/main/lib/auth/default_current_user_provider.rb#L243-L253>

There was a bug for a few months which caused last\_seen to be updated erroneously for API requests.

---

<div class="post-metadata">

### Author: ![jandres](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jandres/32/130933_2.png) [@jandres](https://meta.discourse.org/u/jandres)
#### Post date: [February 6, 2019, 8:26pm UTC](https://meta.discourse.org/t/request-from-api-doesnt-update-user-last-seen-at/108528/3 "2019-02-06T20:26:20Z")

</div>

Very well

Thanks @david for your soon response.

So now how do i update my stats since almost 100% of my forum activities are made by the API ?

It exist any endpoint to update the user visits?

---

<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: [February 7, 2019, 9:36am UTC](https://meta.discourse.org/t/request-from-api-doesnt-update-user-last-seen-at/108528/4 "2019-02-07T09:36:54Z")

</div>

> [@jandres](#):
>
> It exist any endpoint to update the user visits?

No, there isn’t one. If you have control over the API server you could try and “trick” discourse into updating last seen for the request. From the code I quoted above you can see how it works.

So you could make a request with “X-Requested-With" header containing “XMLHttpRequest” (to trick the `xhr?` bit), and “DISCOURSE\_VISIBLE” as “true”.

Note that this wouldn’t be a “supported” way to use the API, but I don’t foresee any problems.

---

<div class="post-metadata">

### Author: ![jandres](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jandres/32/130933_2.png) [@jandres](https://meta.discourse.org/u/jandres)
#### Post date: [February 15, 2019, 3:58am UTC](https://meta.discourse.org/t/request-from-api-doesnt-update-user-last-seen-at/108528/5 "2019-02-15T03:58:44Z")

</div>

Thank you @david

But just one last question :

> [@david](#):
>
> There was a bug for a few months which caused last\_seen to be updated erroneously for API requests.

I don’t understand why ‘a bug’ since, if a user pull for the last topics, it is not considered as a visit ?

Thank you again for your help

---

<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: [February 15, 2019, 10:19am UTC](https://meta.discourse.org/t/request-from-api-doesnt-update-user-last-seen-at/108528/6 "2019-02-15T10:19:36Z")

</div>

> [@jandres](#):
>
> I don’t understand why ‘a bug’ since, if a user pull for the last topics, it is not considered as a visit ?

The API is typically used for things where users are not present - for example the official Discourse mobile app uses the API to check for new topics in the background.

---

<div class="post-metadata">

### Author: ![eviltrout](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eviltrout/32/5275_2.png) [@eviltrout](https://meta.discourse.org/u/eviltrout)
#### Post date: [February 15, 2019, 3:01pm UTC](https://meta.discourse.org/t/request-from-api-doesnt-update-user-last-seen-at/108528/7 "2019-02-15T15:01:02Z")

</div>

I would expect a user viewing something via API to be the same as if they hit that API via their browser.

I feel like that is an exception to the principle of least surprise.

I understand why that decision was made, but perhaps we could accept a HTTP header or parameter that indicates if we want to treat the user as a regular 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: [February 15, 2019, 3:05pm UTC](https://meta.discourse.org/t/request-from-api-doesnt-update-user-last-seen-at/108528/8 "2019-02-15T15:05:54Z")

</div>

> [@eviltrout](#):
>
> perhaps we could accept a HTTP header or parameter that indicates if we want to treat the user as a regular user

👍 we have the DISCOURSE\_VISIBLE header for XHR requests, we could certainly make that work on API requests as well.

---

<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: [April 15, 2019, 4:36pm UTC](https://meta.discourse.org/t/request-from-api-doesnt-update-user-last-seen-at/108528/10 "2019-04-15T16:36:17Z")

</div>

> [@david](#):
>
> we have the DISCOURSE\_VISIBLE header for XHR requests, we could certainly make that work on API requests as well.

You can now set DISCOURSE\_VISIBLE=“true” on API requests, and Discourse will update the last\_seen time for the user

[https://github.com/discourse/discourse/commit/af86cf46dc003f19ba52f71826f057884f1119ba](https://github.com/discourse/discourse/commit/af86cf46dc003f19ba52f71826f057884f1119ba)

---

<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: [April 16, 2019, 5:00pm UTC](https://meta.discourse.org/t/request-from-api-doesnt-update-user-last-seen-at/108528/11 "2019-04-16T17:00:13Z")

</div>

This topic was automatically closed after 24 hours. New replies are no longer allowed.
