# Private Message Send API

**URL:** https://meta.discourse.org/t/private-message-send-api/27593
**Category:** Support
**Created:** [April 15, 2015, 12:44pm UTC](https://meta.discourse.org/t/private-message-send-api/27593 "2015-04-15T12:44:59Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Stackfish](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/stackfish/32/38440_2.png) [@Stackfish](https://meta.discourse.org/u/Stackfish)
#### Post date: [April 15, 2015, 12:44pm UTC](https://meta.discourse.org/t/private-message-send-api/27593/1 "2015-04-15T12:44:59Z")

</div>

Is it possible to send a private message through the API?

---

<div class="post-metadata">

### Author: ![purldator](https://avatars.discourse-cdn.com/v4/letter/p/bcef8e/32.png) [@purldator](https://meta.discourse.org/u/purldator)
#### Post date: [April 15, 2015, 6:22pm UTC](https://meta.discourse.org/t/private-message-send-api/27593/2 "2015-04-15T18:22:40Z")

</div>

It appears you may only view direct (private) messages, not create them. [The API repository @ GitHub](https://github.com/discourse/discourse_api/blob/master/lib/discourse_api/api/private_messages.rb) shows only viewing abilities (GET).

Why do you want to create a private message via API? What is your scenario for wanting this feature?

---

<div class="post-metadata">

### Author: ![AdamCapriola](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/adamcapriola/32/5192_2.png) [@AdamCapriola](https://meta.discourse.org/u/AdamCapriola)
#### Post date: [April 15, 2015, 8:57pm UTC](https://meta.discourse.org/t/private-message-send-api/27593/3 "2015-04-15T20:57:01Z")

</div>

You can create them. URL structure is sorta like this:

`http://discourse.example.com/posts?api_key=abc123&api_username=system&archetype=private_message&title=title&raw=message&target_usernames=username1,username2`

Send a private message and with Chrome developer tools watch the Network tab. (You can learn a lot of neat functionality doing that.)

---

<div class="post-metadata">

### Author: ![purldator](https://avatars.discourse-cdn.com/v4/letter/p/bcef8e/32.png) [@purldator](https://meta.discourse.org/u/purldator)
#### Post date: [April 15, 2015, 9:32pm UTC](https://meta.discourse.org/t/private-message-send-api/27593/4 "2015-04-15T21:32:35Z")

</div>

The API key can be used via URL? That doesn’t sound safe.

The API + curl allows something to be posted at the server level, bypassing the client/browser layer, which is safer than exposing the API key via html link. Someone only needs to check the dev tools as you did and grab it.

However, this depends on what exactly @Stackfish wants this for.

---

<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: [April 15, 2015, 10:28pm UTC](https://meta.discourse.org/t/private-message-send-api/27593/5 "2015-04-15T22:28:14Z")

</div>

A PM is just a type of post, you can create a PM just fine using the API. In fact we are using the API on this very site to create PMs.

PM stands for personal message by the way

---

<div class="post-metadata">

### Author: ![purldator](https://avatars.discourse-cdn.com/v4/letter/p/bcef8e/32.png) [@purldator](https://meta.discourse.org/u/purldator)
#### Post date: [April 15, 2015, 11:47pm UTC](https://meta.discourse.org/t/private-message-send-api/27593/6 "2015-04-15T23:47:15Z")

</div>

But via URL or curl? A URL would expose the API key, correct?

---

<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: [April 15, 2015, 11:49pm UTC](https://meta.discourse.org/t/private-message-send-api/27593/7 "2015-04-15T23:49:45Z")

</div>

Via Discourse api gem or curl or whatever.

---

<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: [April 16, 2015, 5:19am UTC](https://meta.discourse.org/t/private-message-send-api/27593/8 "2015-04-16T05:19:00Z")

</div>

> [@purldator](#):
>
> A URL would expose the API key, correct?

Only if it’s transported over unsecured HTTP, in which case an intercepting observer could have grabbed the form data anyways.

---

<div class="post-metadata">

### Author: ![Stackfish](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/stackfish/32/38440_2.png) [@Stackfish](https://meta.discourse.org/u/Stackfish)
#### Post date: [April 16, 2015, 12:31pm UTC](https://meta.discourse.org/t/private-message-send-api/27593/9 "2015-04-16T12:31:49Z")

</div>

@AdamCapriola Thanks that’s a cool trick and that should meet my needs.

@purldator The reason I’m looking into this is that we are using Discourse as the forum for a larger community. We would like to use discourse as the primary mechanism for messaging and alerts within in our community.

For example, it would be really cool if Stackfish could send a notification (or private message) every time one of our user’s gets a buddy request or one of their stories in liked. These notifications would be system generated.

Thanks to everyone!

---

<div class="post-metadata">

### Author: ![depeters](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/depeters/32/137579_2.png) [@depeters](https://meta.discourse.org/u/depeters)
#### Post date: [May 24, 2015, 8:08pm UTC](https://meta.discourse.org/t/private-message-send-api/27593/10 "2015-05-24T20:08:59Z")

</div>

Is this a POST or a GET? I tried sending this as a GET and I receive a 200 response but no message is created. Does this still work?

---

<div class="post-metadata">

### Author: ![depeters](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/depeters/32/137579_2.png) [@depeters](https://meta.discourse.org/u/depeters)
#### Post date: [May 24, 2015, 8:29pm UTC](https://meta.discourse.org/t/private-message-send-api/27593/11 "2015-05-24T20:29:34Z")

</div>

I just needed to send the request as a POST

---

<div class="post-metadata">

### Author: ![Stackfish](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/stackfish/32/38440_2.png) [@Stackfish](https://meta.discourse.org/u/Stackfish)
#### Post date: [May 25, 2015, 10:10am UTC](https://meta.discourse.org/t/private-message-send-api/27593/12 "2015-05-25T10:10:40Z")

</div>

Sorry I haven’t gotten around to implementing this yet so I’m not sure.

---

<div class="post-metadata">

### Author: ![tobiaseigen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tobiaseigen/32/539204_2.png) [@tobiaseigen](https://meta.discourse.org/u/tobiaseigen)
#### Post date: [November 4, 2017, 7:53pm UTC](https://meta.discourse.org/t/private-message-send-api/27593/13 "2017-11-04T19:53:34Z")

</div>

Hi Adam! Trust all is well with you.

Do you know if there a way to auto archive messages sent via API? Or if not, could such an option be added to the API @codinghorror?

We are getting alot of these API generated messages and we only need to see them when they are replied to, which would put them back in the inbox. For now we are manually selecting all and archiving them. It would be nice to skip that manual step.

---

<div class="post-metadata">

### Author: ![BrianGillespie](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/briangillespie/32/105563_2.png) [@BrianGillespie](https://meta.discourse.org/u/BrianGillespie)
#### Post date: [November 29, 2017, 1:06am UTC](https://meta.discourse.org/t/private-message-send-api/27593/14 "2017-11-29T01:06:20Z")

</div>

Since you are creating private message via API call, you could also change the topic status:

It looks like `status=archived` and `enabled=true` sets the archived flag.

> <https://github.com/discourse/discourse_api/blob/main/examples/change_topic_status.rb>

---

<div class="post-metadata">

### Author: ![tobiaseigen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tobiaseigen/32/539204_2.png) [@tobiaseigen](https://meta.discourse.org/u/tobiaseigen)
#### Post date: [November 29, 2017, 1:54am UTC](https://meta.discourse.org/t/private-message-send-api/27593/15 "2017-11-29T01:54:31Z")

</div>

awesome! Thanks for looking into this. I tried it though, and unless I’m getting something wrong I think this is not working for personal messages - maybe the statuses work differently for messages than topics?

---

<div class="post-metadata">

### Author: ![BrianGillespie](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/briangillespie/32/105563_2.png) [@BrianGillespie](https://meta.discourse.org/u/BrianGillespie)
#### Post date: [November 29, 2017, 4:43am UTC](https://meta.discourse.org/t/private-message-send-api/27593/16 "2017-11-29T04:43:20Z")

</div>

I’m sorry to hear that! I’ve been looking into the APIs for a similar purpose - feeding private messages into the system and managing them. It sounds like there are some bugs that might make this difficult - did you report that status changes didn’t work on PMs?

---

<div class="post-metadata">

### Author: ![tobiaseigen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tobiaseigen/32/539204_2.png) [@tobiaseigen](https://meta.discourse.org/u/tobiaseigen)
#### Post date: [November 29, 2017, 2:30pm UTC](https://meta.discourse.org/t/private-message-send-api/27593/17 "2017-11-29T14:30:35Z")

</div>

Hi Brian! I don’t think this is a bug necessarily - more that I am not understanding the difference between messages and topics. Unlike topics, which can be archived for everyone, messages have an inbox and an archive for each user (and for each group) included in the message. So there’s likely another field for tracking that for each user.

I had a quick dig in the [data explorer](https://meta.discourse.org/t/32566?silent=true) and indeed see there are `user_archived_messages` and `group_archived_messages` tables but don’t really know what that means or how they work, or if it’s actually possible via the API to place an outgoing message in the sender’s archive or a group’s archive by default but in the inbox for the intended recipient.

Anyhoo this is not the highest priority for me as I have found a workflow with the current setup that seems to work for my team. If you do figure it out, let me know! 🙂

---

<div class="post-metadata">

### Author: ![sarahann](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sarahann/32/96703_2.png) [@sarahann](https://meta.discourse.org/u/sarahann)
#### Post date: [July 10, 2018, 9:12pm UTC](https://meta.discourse.org/t/private-message-send-api/27593/18 "2018-07-10T21:12:20Z")

</div>

What if you use an email template as a message?

I am going to the email\_templates endpoint, grabbing the template I want, then send that as a private message. However, none of the strings are replaced in the message to the user. The message contains %{site\_name} and ${base\_url} inside the message text.

Just wondering how/when in the flow does Discourse substitute these (I couldn’t find any evidence).

---

<div class="post-metadata">

### Author: ![littleviolette](https://avatars.discourse-cdn.com/v4/letter/l/d26b3c/32.png) [@littleviolette](https://meta.discourse.org/u/littleviolette)
#### Post date: [December 22, 2020, 5:41am UTC](https://meta.discourse.org/t/private-message-send-api/27593/19 "2020-12-22T05:41:33Z")

</div>

Swapped out the `api_key` and site name, entered it into Chrome and got a 406 error. I assume I am applying it wrong (sorry not a programmer)? 😕

---

<div class="post-metadata">

### Author: ![RogerBW](https://avatars.discourse-cdn.com/v4/letter/r/b4bc9f/32.png) [@RogerBW](https://meta.discourse.org/u/RogerBW)
#### Post date: [April 26, 2021, 12:23pm UTC](https://meta.discourse.org/t/private-message-send-api/27593/20 "2021-04-26T12:23:31Z")

</div>

I’m attempting to send a PM through the API. (This is intended for a game in which each player is dealt a hand of cards, so it does need to be private.)

I’m sending a request with appropriate values for `raw`, `title` and `target_usernames`, and getting back `{"action":"create_post","errors":["Category can't be blank"]}` with a `422 Unprocessable Entity`.

If I set a category, it ignores `target_usernames` and creates a topic in that category.

If someone is successfully creating and sending PMs using the API, could you post an example with your full request body schema?

[Next page](https://meta.discourse.org/t/private-message-send-api/27593.md?page=2)
