> ## 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.

# Spaces

> Understanding Spaces in the Mighty Networks API

## What are Spaces?

**Spaces** are organizational units within a network where content and members are organized. Think of them as communities, groups, or channels within your larger network.

Spaces help you:

* Organize content by topic or purpose
* Control access and permissions
* Create focused communities within your network
* Manage members at a granular level

## Space Properties

Each space includes:

* **Space ID** - Unique identifier for the space
* **Name** - Display name of the space
* **Visibility** - Public, private, or secret
* **Members** - Users who have access to this space
* **Posts** - Content created within the space
* **Settings** - Configuration for moderation, features, etc.

## Listing Spaces

Get all spaces in your network:

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

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

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

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

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

### Example Response

```json theme={null}
{
  "items": [
    {
      "id": 12345,
      "name": "General Discussion",
      "created_at": "2024-01-15T10:30:00Z",
      "updated_at": "2024-03-20T14:22:00Z"
    },
    {
      "id": 12346,
      "name": "Announcements",
      "created_at": "2024-01-15T10:35:00Z",
      "updated_at": "2024-03-19T09:15:00Z"
    }
  ],
  "links": {
    "self": "https://api.mn.co/admin/v1/networks/123/spaces?page=1",
    "next": "https://api.mn.co/admin/v1/networks/123/spaces?page=2"
  }
}
```

## Getting a Specific Space

Retrieve details about a single space:

<CodeGroup>
  ```bash cURL theme={null}
  curl https://api.mn.co/admin/v1/networks/{network_id}/spaces/{space_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}/spaces/${SPACE_ID}`,
    {
      headers: {
        'Authorization': `Bearer ${API_TOKEN}`
      }
    }
  );

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

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

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

## Space Membership

### Listing Members in a Space

Get all members who have access to a specific space:

```bash theme={null}
curl https://api.mn.co/admin/v1/networks/{network_id}/spaces/{space_id}/members \
  -H "Authorization: Bearer YOUR_API_TOKEN"
```

### Adding Members to a Space

Add a user to a space:

```bash theme={null}
curl -X POST https://api.mn.co/admin/v1/networks/{network_id}/spaces/{space_id}/members?user_id={user_id} \
  -H "Authorization: Bearer YOUR_API_TOKEN"
```

### Removing Members from a Space

Remove a user from a space:

```bash theme={null}
curl -X DELETE https://api.mn.co/admin/v1/networks/{network_id}/spaces/{space_id}/members/{user_id}/ \
  -H "Authorization: Bearer YOUR_API_TOKEN"
```

## Space Roles

Members can have different roles within a space:

* **Host** - Can manage space settings, content, and members
* **Member** - Standard access to view and create content
* **Moderator** - Can moderate content but not change settings

### Updating Member Roles

Change a member's role in a space:

<CodeGroup>
  ```bash cURL theme={null}
  curl -X PATCH https://api.mn.co/admin/v1/networks/{network_id}/spaces/{space_id}/members/{user_id}/ \
    -H "Authorization: Bearer YOUR_API_TOKEN" \
    -H "Content-Type: application/json" \
    -d '{"role": "host"}'
  ```

  ```javascript Node.js theme={null}
  const response = await fetch(
    `https://api.mn.co/admin/v1/networks/${NETWORK_ID}/spaces/${SPACE_ID}/members/${USER_ID}/`,
    {
      method: 'PATCH',
      headers: {
        'Authorization': `Bearer ${API_TOKEN}`,
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({ role: 'host' })
    }
  );
  ```
</CodeGroup>

## Common Use Cases

### Bulk Member Assignment

Add multiple users to a space:

```javascript theme={null}
async function addMembersToSpace(networkId, spaceId, userIds, apiToken) {
  const results = [];

  for (const userId of userIds) {
    try {
      const response = await fetch(
        `https://api.mn.co/admin/v1/networks/${networkId}/spaces/${spaceId}/members?user_id=${userId}`,
        {
          method: 'POST',
          headers: { 'Authorization': `Bearer ${apiToken}` }
        }
      );

      if (response.ok) {
        results.push({ userId, success: true });
      }
    } catch (error) {
      results.push({ userId, success: false, error: error.message });
    }
  }

  return results;
}
```

### Space Analytics

Get member count for each space:

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

  const spaces = await spacesResponse.json();

  const analytics = [];
  for (const space of spaces.items) {
    const membersResponse = await fetch(
      `https://api.mn.co/admin/v1/networks/${networkId}/spaces/${space.id}/members`,
      { headers: { 'Authorization': `Bearer ${apiToken}` } }
    );

    const members = await membersResponse.json();
    analytics.push({
      spaceName: space.name,
      memberCount: members.items?.length || 0
    });
  }

  return analytics;
}
```

### Automated Space Assignment

Automatically assign new members to a default space:

```javascript theme={null}
async function assignToDefaultSpace(networkId, spaceId, newMemberId, apiToken) {
  const response = await fetch(
    `https://api.mn.co/admin/v1/networks/${networkId}/spaces/${spaceId}/members?user_id=${newMemberId}`,
    {
      method: 'POST',
      headers: { 'Authorization': `Bearer ${apiToken}` }
    }
  );

  if (!response.ok) {
    throw new Error(`Failed to add member to space: ${response.statusText}`);
  }

  return await response.json();
}
```

## Best Practices

1. **Organize Content Logically** - Use spaces to group related content and discussions
2. **Manage Permissions Carefully** - Consider who should have access to each space
3. **Use Pagination** - Always paginate when listing space members
4. **Handle Errors** - Check for 404 errors if a space doesn't exist
5. **Respect Rate Limits** - Be mindful when performing bulk operations across spaces

## Next Steps

<CardGroup cols={2}>
  <Card title="Members" icon="users" href="/members">
    Learn about member management and roles.
  </Card>

  <Card title="Posts" icon="file-lines" href="/posts">
    Understand how to work with posts and content.
  </Card>

  <Card title="Networks" icon="network-wired" href="/networks">
    Learn about network-level operations.
  </Card>

  <Card title="Admin API" icon="code" href="/admin-api">
    Explore all space-related endpoints.
  </Card>
</CardGroup>
