# Updating email preferences via API

**URL:** https://meta.discourse.org/t/updating-email-preferences-via-api/65983
**Category:** Support
**Created:** [2017 年 7 月 11 日午前 5:10 UTC](https://meta.discourse.org/t/updating-email-preferences-via-api/65983 "2017-07-11T05:10:28Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![kgoetz](https://avatars.discourse-cdn.com/v4/letter/k/bb73d2/32.png) [@kgoetz](https://meta.discourse.org/u/kgoetz)
#### Post date: [2017 年 7 月 11 日午前 5:10 UTC](https://meta.discourse.org/t/updating-email-preferences-via-api/65983/1 "2017-07-11T05:10:28Z")

</div>

Hi,  
I’ve spent several hours trying to update users email preferences via the api, and haven’t had much luck. Specifically, I want to enable Activity Summary and turn of Mailing list mode.

Has anyone done this and succeeded and can point me at where I’ve gone wrong?

Some examples below.

I tried this with and without the user/user option layers. Unfortunately there isn’t a meaningful error, just a page of html spat out.

> $ curl -X POST --data ‘user: { user\_option: {“mailing\_list\_mode\_frequency”: 1, “email\_direct”: True, “mailing\_list\_mode”: False, “email\_digests”: True, “email\_in\_reply\_to”: True, “enable\_quoting”: True, “like\_notification\_frequency”: 1, “email\_always”: True, “email\_private\_messages”: True, “email\_previous\_replies”: 1, “include\_tl0\_in\_digests”: True}}}’ “[https://discuss.example.com/users/karl\_goetz?api\_key=[..]&api\_username=karl\_goetz](https://discuss.example.com/users/karl_goetz?api_key=%5B..%5D&api_username=karl_goetz)”
> 
> Forums

Using pydiscuourse (installed from pip), has also failed. Some examples of me finding with the options:

> discourse\_client.set\_preference(mailing\_list\_mode=False, email\_digests=True, email\_direct=True, email\_in\_reply\_to=True, email\_private\_messages=True, digest\_after\_minutes=1440, include\_tl0\_in\_digests=True)

> discourse\_client.set\_preference(‘karl\_goetz’, email\_always=True, mailing\_list\_mode=False, email\_digests=True, email\_direct=True, email\_in\_reply\_to=False, email\_private\_messages=True, email\_previous\_replies=1, digest\_after\_minutes=1440, include\_tl0\_in\_digests=True)

I can change my user preferences via chrome at [example.com/u/karl\_goetz/preferences/emails](http://example.com/u/karl_goetz/preferences/emails) so _that_ bit works and if nothing else I’ll try pushing data in through that route.

---

<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: [2017 年 7 月 11 日午後 2:51 UTC](https://meta.discourse.org/t/updating-email-preferences-via-api/65983/2 "2017-07-11T14:51:59Z")

</div>

You will need to send a PUT request to `/u/{{username}}.json`

```
email_always:true
mailing_list_mode:false
mailing_list_mode_frequency:1
email_digests:true
email_direct:true
email_in_reply_to:true
email_private_messages:true
email_previous_replies:2
digest_after_minutes:10080
include_tl0_in_digests:false

```

---

<div class="post-metadata">

### Author: ![kgoetz](https://avatars.discourse-cdn.com/v4/letter/k/bb73d2/32.png) [@kgoetz](https://meta.discourse.org/u/kgoetz)
#### Post date: [2017 年 7 月 14 日午前 2:43 UTC](https://meta.discourse.org/t/updating-email-preferences-via-api/65983/3 "2017-07-14T02:43:52Z")

</div>

Thanks @blake, I think that has me back on the right path.

> curl -X PUT “[https://discuss.example.com/u/karl\_goetz.json?api\_key=[...]&api\_username=karl\_goetz&mailing\_list\_mode=false&email\_digests=true](https://discuss.example.com/u/karl_goetz.json?api_key=%5B...%5D&api_username=karl_goetz&mailing_list_mode=false&email_digests=true)”

Now when I refresh the web my settings have changed from mailing list mode to activity summary.

After more annoyance and reporting to reading the pydisuourse source it appears the \_PUT method doesn’t support passing params so I used their \_request method directly.

> d  
> {‘email\_always’: ‘true’, ‘mailing\_list\_mode\_frequency’: 1, ‘email\_private\_messages’: ‘true’, ‘email\_direct’: ‘true’, ‘mailing\_list\_mode’: ‘false’, ‘digest\_after\_minutes’: 1440, ‘email\_digests’: ‘true’, ‘include\_tl0\_in\_digests’: False, ‘email\_in\_reply\_to’: ‘true’, ‘email\_previous\_replies’: 1, ‘api\_username’: ‘karl\_goetz’, ‘api\_key’: ‘[…]’}  
> discourse\_client.\_request(‘PUT’, ‘/u/karl\_goetz.json’, params=d)

Which behaved as expected.

Thanks again for your assistance.

PS.  
Do you know why some user settings are only accessible via /u/username not /users/username ?

---

<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: [2017 年 7 月 14 日午後 11:27 UTC](https://meta.discourse.org/t/updating-email-preferences-via-api/65983/4 "2017-07-14T23:27:52Z")

</div>

At some point we renamed the `/users` endpoint to `/u` so that we can have pretty short urls in the browser like we do for topics and categories. If you do

```
$ rake routes | grep username

```

you will see most of the routes and I think currently most of the `/users` ones have duplicate `/u` routes. The line is generating the `/u` routes:

> <https://github.com/discourse/discourse/blob/main/config/routes.rb#L311>

but I believe there are still some `/users` routes that don’t have a matching `/u` route.

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [2017 年 7 月 15 日午前 12:31 UTC](https://meta.discourse.org/t/updating-email-preferences-via-api/65983/5 "2017-07-15T00:31:19Z")

</div>

We should figure out what those are, and fix them so the `/u` works as expected..

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [2024 年 6 月 8 日午後 12:44 UTC](https://meta.discourse.org/t/updating-email-preferences-via-api/65983/6 "2024-06-08T12:44:28Z")

</div>

このトピックは2524日後に自動的に閉じられました。返信はもう許可されていません。
