# Unable to send a PM via DiscourseApi - "errors"=\>\["You must select a valid user."\]

**URL:** https://meta.discourse.org/t/unable-to-send-a-pm-via-discourseapi-errors-you-must-select-a-valid-user/282943
**Category:** Development
**Tags:** rest-api
**Created:** [October 20, 2023, 9:07pm UTC](https://meta.discourse.org/t/unable-to-send-a-pm-via-discourseapi-errors-you-must-select-a-valid-user/282943 "2023-10-20T21:07:13Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![AstonJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/astonj/32/215041_2.png) [@AstonJ](https://meta.discourse.org/u/AstonJ)
#### Post date: [October 20, 2023, 9:07pm UTC](https://meta.discourse.org/t/unable-to-send-a-pm-via-discourseapi-errors-you-must-select-a-valid-user/282943/1 "2023-10-20T21:07:13Z")

</div>

This used to work, but recently on trying to send a PM using the DiscourseApi I keep getting: `DiscourseApi::UnprocessableEntity ({"action"=>"create_post", "errors"=>["You must select a valid user."]})` but the user is valid (and the same thing happens no matter which username I try).

Via a console using the Discourse gem:

```plaintext
user = "MyUserName"
client = DiscourseApi::Client.new("https://myforum.com")
client.api_key = "h03873hxjd734i4hdjdudje94udjdjdeje83483483jdjdjdjed84u34e"
client.api_username = "ForumUserName"

client.create_private_message(
  title: "testing a title",
  raw: "Hey this is a test",
  target_usernames: user
)

```

Looks like a bug? (Think there was a similar bug with tags a while back : /)

---

<div class="post-metadata">

### Author: ![Arkshine](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/arkshine/32/298682_2.png) [@Arkshine](https://meta.discourse.org/u/Arkshine)
#### Post date: [October 20, 2023, 9:35pm UTC](https://meta.discourse.org/t/unable-to-send-a-pm-via-discourseapi-errors-you-must-select-a-valid-user/282943/2 "2023-10-20T21:35:33Z")

</div>

Hi Aston,

It looks like `target_usernames` has been deprecated.

Try `target_recipients` instead! 🙂

> **[Discourse API Docs](https://docs.discourse.org/#tag/Private-Messages)**

---

<div class="post-metadata">

### Author: ![AstonJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/astonj/32/215041_2.png) [@AstonJ](https://meta.discourse.org/u/AstonJ)
#### Post date: [January 10, 2024, 6:05am UTC](https://meta.discourse.org/t/unable-to-send-a-pm-via-discourseapi-errors-you-must-select-a-valid-user/282943/3 "2024-01-10T06:05:54Z")

</div>

Just a note to say you need to use `create_pm` as well (instead of `create_private_message`).

---

<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: [February 9, 2024, 6:06am UTC](https://meta.discourse.org/t/unable-to-send-a-pm-via-discourseapi-errors-you-must-select-a-valid-user/282943/4 "2024-02-09T06:06:45Z")

</div>

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