# How do I extract posts from a Mattermost channel?

**URL:** <https://forum.mattermost.com/t/how-do-i-extract-posts-from-a-mattermost-channel/6285>\
**Category:** Troubleshooting\
**Created:** [December 3, 2018, 1:45pm UTC](https://forum.mattermost.com/t/how-do-i-extract-posts-from-a-mattermost-channel/6285 "2018-12-03T13:45:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![cjohannsen81](https://avatars.discourse-cdn.com/v4/letter/c/a88e57/32.png) [@cjohannsen81](https://forum.mattermost.com/u/cjohannsen81)\
**Post date:** [December 3, 2018, 1:45pm UTC](https://forum.mattermost.com/t/how-do-i-extract-posts-from-a-mattermost-channel/6285/1 "2018-12-03T13:45:06Z")

</div>

Last week I faced the challenge to extract user posts from Mattermost without using the database. I checked the Mattermost API documentation and came up with a few simple curl commands to achieve this. You can easily build this into a python script or pipe the result into a JSON file to process it afterwards.

Here is what I did:

```
# Please replace the capital letter words with your environment, tokens etc.
# Login using your credentials (System-Admin)
curl -i -d '{"login_id":"USERNAME","password":"PASSWORD"}' https://MATTERMOST/api/v4/users/login

# Gives you a token for next operations like this:
token: wqgwrca75ibumynq7p1kpzbutw

# Search the team to get the team ID
curl -i -H 'Authorization: Bearer TOKEN' -X POST -d '{ "term": "TEAMNAME"}' https://MATTERMOST/api/v4/teams/search

# Gives you a team ID like this:
"id":"trwd6spc8fd65pymp3dy8rtg4o"

# Search a channel in the team you want to get the posts from:
curl -i -H 'Authorization: Bearer TOKEN' -X POST -d '{ "term": "CHANNELNAME"}' https://MATTERMOST/api/v4/teams/TEAMID/channels/search

# Gives you a channel ID like this:
"id":"z8d65yhzxfrh5nzadyp9aa56qw"

# Get the posts from the channel
curl -i -H 'Authorization: Bearer TOKEN' https://MATTERMOST/api/v4/channels/CHANNELID/posts

```

Hope this helps!  
Christian

---

<div class="post-metadata">

**Author:** ![ahmaddanial](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mattermost.com/ahmaddanial/32/2938_2.png) [@ahmaddanial](https://forum.mattermost.com/u/ahmaddanial)\
**Post date:** [July 24, 2019, 5:12am UTC](https://forum.mattermost.com/t/how-do-i-extract-posts-from-a-mattermost-channel/6285/2 "2019-07-24T05:12:41Z")

</div>

On top of the suggestion by @cjohannsen81, you might also be interested to give the database a try (if you don’t prefer to use the API) by using the following query:

```
SELECT
Teams.DisplayName AS Team,
Channels.DisplayName AS Channel,
Channels.Type,
(SELECT COUNT(*) FROM mattermost.Posts WHERE Posts.ChannelId = Channels.Id AND Posts.DeleteAt = 0) AS Posts
FROM
mattermost.Channels
JOIN
mattermost.Teams ON Channels.TeamId = Teams.Id
WHERE
Channels.DeleteAt = 0 AND
Channels.Type <> 'D' AND
Teams.DeleteAt = 0
ORDER BY
Team, Channel;

```

Credits to **Craig Vitter** for sharing this in the [get\_posts\_by\_team\_and\_channel.sql](https://github.com/cvitter/mattermost-scripts/blob/master/get_posts_by_team_and_channel.sql) page.
