> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mightynetworks.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Posts

> Understanding Posts and Content in the Mighty Networks API

## What are Posts?

**Posts** are the primary content type in Mighty Networks. They represent user-generated content including text, images, videos, and other media shared within your network.

Posts can be:

* Created in specific spaces
* Visible to the entire network or specific spaces
* Commented on and reacted to
* Moderated by admins and hosts

## Post Properties

Each post includes:

* **Post ID** - Unique identifier
* **Content** - Text content with rich formatting
* **Author** - Member who created the post
* **Space** - Where the post was created
* **Media** - Attached images, videos, or files
* **Timestamps** - Created and updated dates
* **Engagement** - Comments, reactions, and views
* **Visibility** - Public, members-only, or space-specific

## Listing Posts

Get all posts in your network:

<CodeGroup>
  ```bash cURL theme={null}
  curl https://api.mn.co/admin/v1/networks/{network_id}/posts \
    -H "Authorization: Bearer YOUR_API_TOKEN"
  ```

  ```javascript Node.js theme={null}
  const response = await fetch(
    `https://api.mn.co/admin/v1/networks/${NETWORK_ID}/posts`,
    {
      headers: {
        'Authorization': `Bearer ${API_TOKEN}`
      }
    }
  );

  const posts = await response.json();
  console.log(posts.items);
  ```

  ```python Python theme={null}
  response = requests.get(
      f"https://api.mn.co/admin/v1/networks/{NETWORK_ID}/posts",
      headers={"Authorization": f"Bearer {API_TOKEN}"}
  )

  posts = response.json()
  print(posts['items'])
  ```
</CodeGroup>

### Example Response

```json theme={null}
{
  "items": [
    {
      "id": 98765,
      "body": "Welcome to our community! Excited to connect with everyone.",
      "author_id": 12345,
      "space_id": 456,
      "created_at": "2024-03-20T14:30:00Z",
      "updated_at": "2024-03-20T14:30:00Z",
      "comments_count": 5,
      "reactions_count": 12
    }
  ],
  "links": {
    "self": "https://api.mn.co/admin/v1/networks/123/posts?page=1",
    "next": "https://api.mn.co/admin/v1/networks/123/posts?page=2"
  }
}
```

## Getting a Specific Post

Retrieve details about a single post:

<CodeGroup>
  ```bash cURL theme={null}
  curl https://api.mn.co/admin/v1/networks/{network_id}/posts/{post_id}/ \
    -H "Authorization: Bearer YOUR_API_TOKEN"
  ```

  ```javascript Node.js theme={null}
  const response = await fetch(
    `https://api.mn.co/admin/v1/networks/${NETWORK_ID}/posts/${POST_ID}/`,
    {
      headers: {
        'Authorization': `Bearer ${API_TOKEN}`
      }
    }
  );

  const post = await response.json();
  ```

  ```python Python theme={null}
  response = requests.get(
      f"https://api.mn.co/admin/v1/networks/{NETWORK_ID}/posts/{POST_ID}/",
      headers={"Authorization": f"Bearer {API_TOKEN}"}
  )

  post = response.json()
  ```
</CodeGroup>

## Creating Posts

Create a new post in your network:

<CodeGroup>
  ```bash cURL theme={null}
  curl -X POST https://api.mn.co/admin/v1/networks/{network_id}/posts \
    -H "Authorization: Bearer YOUR_API_TOKEN" \
    -H "Content-Type: application/json" \
    -d '{
      "body": "This is my post content",
      "space_id": 456
    }'
  ```

  ```javascript Node.js theme={null}
  const response = await fetch(
    `https://api.mn.co/admin/v1/networks/${NETWORK_ID}/posts`,
    {
      method: 'POST',
      headers: {
        'Authorization': `Bearer ${API_TOKEN}`,
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        body: 'This is my post content',
        space_id: 456
      })
    }
  );

  const newPost = await response.json();
  ```

  ```python Python theme={null}
  response = requests.post(
      f"https://api.mn.co/admin/v1/networks/{NETWORK_ID}/posts",
      headers={
          "Authorization": f"Bearer {API_TOKEN}",
          "Content-Type": "application/json"
      },
      json={
          "body": "This is my post content",
          "space_id": 456
      }
  )

  new_post = response.json()
  ```
</CodeGroup>

### Creating Posts with Notifications

You can optionally notify network members when creating a post:

```bash theme={null}
curl -X POST "https://api.mn.co/admin/v1/networks/{network_id}/posts?notify=true" \
  -H "Authorization: Bearer YOUR_API_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "body": "Important announcement for all members!",
    "space_id": 456
  }'
```

<Note>
  Use the `notify=true` parameter to send push notifications and emails to network members about the new post.
</Note>

## Common Use Cases

### Automated Announcements

Post automated announcements to your network:

```javascript theme={null}
async function postAnnouncement(networkId, spaceId, message, apiToken) {
  const response = await fetch(
    `https://api.mn.co/admin/v1/networks/${networkId}/posts?notify=true`,
    {
      method: 'POST',
      headers: {
        'Authorization': `Bearer ${apiToken}`,
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        body: message,
        space_id: spaceId
      })
    }
  );

  if (!response.ok) {
    throw new Error(`Failed to create post: ${response.statusText}`);
  }

  return await response.json();
}

// Usage
await postAnnouncement(
  NETWORK_ID,
  ANNOUNCEMENTS_SPACE_ID,
  '🎉 New feature launched! Check out our latest update.',
  API_TOKEN
);
```

### Content Moderation

Monitor recent posts for moderation:

```javascript theme={null}
async function getRecentPosts(networkId, hours, apiToken) {
  const response = await fetch(
    `https://api.mn.co/admin/v1/networks/${networkId}/posts`,
    { headers: { 'Authorization': `Bearer ${apiToken}` } }
  );

  const posts = await response.json();

  const cutoffTime = Date.now() - (hours * 60 * 60 * 1000);

  return posts.items.filter(post => {
    const postTime = new Date(post.created_at).getTime();
    return postTime >= cutoffTime;
  });
}

// Get posts from last 24 hours
const recentPosts = await getRecentPosts(NETWORK_ID, 24, API_TOKEN);
```

### Engagement Analytics

Track post engagement metrics:

```javascript theme={null}
async function getPostEngagement(networkId, apiToken) {
  const response = await fetch(
    `https://api.mn.co/admin/v1/networks/${networkId}/posts`,
    { headers: { 'Authorization': `Bearer ${apiToken}` } }
  );

  const posts = await response.json();

  return posts.items.map(post => ({
    id: post.id,
    body: post.body.substring(0, 50) + '...',
    comments: post.comments_count || 0,
    reactions: post.reactions_count || 0,
    engagement: (post.comments_count || 0) + (post.reactions_count || 0),
    created: post.created_at
  })).sort((a, b) => b.engagement - a.engagement);
}
```

### Scheduled Posts

Create a simple scheduled posting system:

```javascript theme={null}
async function schedulePost(networkId, spaceId, content, scheduleTime, apiToken) {
  const delay = new Date(scheduleTime).getTime() - Date.now();

  if (delay < 0) {
    throw new Error('Schedule time must be in the future');
  }

  setTimeout(async () => {
    await fetch(
      `https://api.mn.co/admin/v1/networks/${networkId}/posts?notify=true`,
      {
        method: 'POST',
        headers: {
          'Authorization': `Bearer ${apiToken}`,
          'Content-Type': 'application/json'
        },
        body: JSON.stringify({
          body: content,
          space_id: spaceId
        })
      }
    );
  }, delay);

  console.log(`Post scheduled for ${scheduleTime}`);
}

// Schedule a post for tomorrow at 9 AM
const tomorrow9AM = new Date();
tomorrow9AM.setDate(tomorrow9AM.getDate() + 1);
tomorrow9AM.setHours(9, 0, 0, 0);

await schedulePost(
  NETWORK_ID,
  SPACE_ID,
  'Good morning! Here\'s your daily motivation.',
  tomorrow9AM,
  API_TOKEN
);
```

### Content Export

Export all posts for backup or analysis:

```javascript theme={null}
async function exportAllPosts(networkId, apiToken) {
  let allPosts = [];
  let page = 1;
  let hasMore = true;

  while (hasMore) {
    const response = await fetch(
      `https://api.mn.co/admin/v1/networks/${networkId}/posts?page=${page}`,
      { headers: { 'Authorization': `Bearer ${apiToken}` } }
    );

    const data = await response.json();
    allPosts = allPosts.concat(data.items);

    hasMore = data.links?.next != null;
    page++;

    // Rate limiting
    await new Promise(resolve => setTimeout(resolve, 100));
  }

  return allPosts;
}
```

## Working with Rich Media

Posts can include various types of media:

### Images

At this time, images can't be added directly via API.

### Videos

Videos can be embedded in posts, either through direct uploads or links to platforms like YouTube or Vimeo.

### Links

URLs in post content are automatically detected and can be previewed with link cards.

## Best Practices

1. **Use Pagination** - Always paginate when fetching multiple posts
2. **Rate Limiting** - Be mindful of rate limits when creating multiple posts
3. **Notify Sparingly** - Only use `notify=true` for important announcements
4. **Validate Content** - Check post content before creating to avoid errors
5. **Handle Errors** - Implement proper error handling for failed post creation
6. **Cache Post Data** - Store frequently accessed posts locally
7. **Monitor Engagement** - Track comments and reactions for content insights

## Next Steps

<CardGroup cols={2}>
  <Card title="Spaces" icon="object-group" href="/spaces">
    Learn where posts are organized.
  </Card>

  <Card title="Members" icon="users" href="/members">
    Understand post authors and engagement.
  </Card>

  <Card title="Pagination" icon="list" href="/api-pagination">
    Master pagination for post lists.
  </Card>

  <Card title="Rate Limits" icon="gauge" href="/admin-api#rate-limit-and-quota">
    Understand API rate limiting.
  </Card>
</CardGroup>
