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

# Service Accounts

> Manage programmatic access to Mixpanel

# Service Accounts API

Manage service accounts for programmatic access to Mixpanel APIs.

## Base URL

```
https://mixpanel.com/api/app
```

## Authentication

Use Service Account credentials with HTTP Basic Auth.

## List Service Accounts

Get all service accounts for your organization.

<ParamField path="organizationId" type="integer" required>
  Your Mixpanel organization ID
</ParamField>

### Example Request

<CodeGroup>
  ```bash cURL theme={null}
  curl "https://mixpanel.com/api/app/organizations/123/service-accounts" \
    -u SERVICE_ACCOUNT_USERNAME:SERVICE_ACCOUNT_SECRET
  ```

  ```python Python theme={null}
  import requests
  from requests.auth import HTTPBasicAuth

  response = requests.get(
      'https://mixpanel.com/api/app/organizations/123/service-accounts',
      auth=HTTPBasicAuth('SERVICE_ACCOUNT_USERNAME', 'SERVICE_ACCOUNT_SECRET')
  )

  accounts = response.json()
  for account in accounts['results']:
      print(f"{account['username']}: ID {account['id']}")
  ```
</CodeGroup>

### Response

<CodeGroup>
  ```json 200 Success theme={null}
  {
    "status": "ok",
    "results": [
      {
        "id": 12345,
        "username": "api-bot",
        "last_used": "2024-01-15T10:30:00Z",
        "expires": "2025-01-15T10:30:00Z",
        "creator": 789,
        "created": "2024-01-01T09:00:00Z",
        "user": 12345
      }
    ]
  }
  ```
</CodeGroup>

***

## Create Service Account

Create a new service account for your organization.

<ParamField path="organizationId" type="integer" required>
  Your Mixpanel organization ID
</ParamField>

<ParamField body="username" type="string" required>
  A descriptive name for the service account
</ParamField>

<ParamField body="role" type="string">
  The service account's role

  Options: `owner`, `admin`, `analyst`, `consumer`
</ParamField>

<ParamField body="expires" type="string">
  Expiration date and time in ISO format

  Example: `2025-12-31T23:59:59Z`
</ParamField>

<ParamField body="projects" type="array">
  List of projects to add the service account to

  <Expandable title="projects[]">
    <ParamField body="id" type="integer">
      The project ID
    </ParamField>

    <ParamField body="role" type="string">
      Role for this project: `owner`, `admin`, `analyst`, `consumer`
    </ParamField>
  </Expandable>
</ParamField>

### Example Request

<CodeGroup>
  ```bash cURL theme={null}
  curl "https://mixpanel.com/api/app/organizations/123/service-accounts" \
    -X POST \
    -u SERVICE_ACCOUNT_USERNAME:SERVICE_ACCOUNT_SECRET \
    -H "Content-Type: application/json" \
    -d '{
      "username": "data-pipeline-bot",
      "role": "admin",
      "expires": "2025-12-31T23:59:59Z",
      "projects": [
        {"id": 456, "role": "admin"},
        {"id": 789, "role": "analyst"}
      ]
    }'
  ```

  ```python Python theme={null}
  import requests
  from requests.auth import HTTPBasicAuth

  response = requests.post(
      'https://mixpanel.com/api/app/organizations/123/service-accounts',
      auth=HTTPBasicAuth('SERVICE_ACCOUNT_USERNAME', 'SERVICE_ACCOUNT_SECRET'),
      json={
          'username': 'data-pipeline-bot',
          'role': 'admin',
          'expires': '2025-12-31T23:59:59Z',
          'projects': [
              {'id': 456, 'role': 'admin'},
              {'id': 789, 'role': 'analyst'}
          ]
      }
  )

  result = response.json()
  print(f"Service Account ID: {result['results']['id']}")
  print(f"Secret (save this!): {result['results']['token']}")
  ```
</CodeGroup>

### Response

<CodeGroup>
  ```json 201 Created theme={null}
  {
    "status": "ok",
    "results": {
      "id": 12345,
      "username": "data-pipeline-bot",
      "token": "SECRET_TOKEN_SAVE_THIS",
      "last_used": null,
      "expires": "2025-12-31T23:59:59Z",
      "creator": 789,
      "created": "2024-01-15T10:30:00Z",
      "user": 12345
    }
  }
  ```
</CodeGroup>

<Warning>
  The `token` (secret) is only returned once during creation. Store it securely - it cannot be retrieved later.
</Warning>

***

## Get Service Account

Retrieve details of a specific service account.

<ParamField path="organizationId" type="integer" required>
  Your Mixpanel organization ID
</ParamField>

<ParamField path="serviceAccountId" type="integer" required>
  The service account ID
</ParamField>

### Example Request

<CodeGroup>
  ```bash cURL theme={null}
  curl "https://mixpanel.com/api/app/organizations/123/service-accounts/12345" \
    -u SERVICE_ACCOUNT_USERNAME:SERVICE_ACCOUNT_SECRET
  ```
</CodeGroup>

***

## Delete Service Account

Remove a service account from your organization.

<ParamField path="organizationId" type="integer" required>
  Your Mixpanel organization ID
</ParamField>

<ParamField path="serviceAccountId" type="integer" required>
  The service account ID to delete
</ParamField>

### Example Request

<CodeGroup>
  ```bash cURL theme={null}
  curl "https://mixpanel.com/api/app/organizations/123/service-accounts/12345" \
    -X DELETE \
    -u SERVICE_ACCOUNT_USERNAME:SERVICE_ACCOUNT_SECRET
  ```

  ```python Python theme={null}
  import requests
  from requests.auth import HTTPBasicAuth

  response = requests.delete(
      'https://mixpanel.com/api/app/organizations/123/service-accounts/12345',
      auth=HTTPBasicAuth('SERVICE_ACCOUNT_USERNAME', 'SERVICE_ACCOUNT_SECRET')
  )

  print(response.json())  # {"status": "ok"}
  ```
</CodeGroup>

***

## Add Service Account to Projects

Add one or more service accounts to one or more projects.

<ParamField path="organizationId" type="integer" required>
  Your Mixpanel organization ID
</ParamField>

<ParamField body="projects" type="array" required>
  List of projects and roles

  <Expandable title="projects[]">
    <ParamField body="id" type="integer">
      The project ID
    </ParamField>

    <ParamField body="role" type="string">
      Role for this project
    </ParamField>
  </Expandable>
</ParamField>

<ParamField body="service_account_ids" type="array" required>
  Array of service account IDs to add
</ParamField>

### Example Request

<CodeGroup>
  ```bash cURL theme={null}
  curl "https://mixpanel.com/api/app/organizations/123/service-accounts/add-to-project" \
    -X POST \
    -u SERVICE_ACCOUNT_USERNAME:SERVICE_ACCOUNT_SECRET \
    -H "Content-Type: application/json" \
    -d '{
      "projects": [
        {"id": 456, "role": "admin"},
        {"id": 789, "role": "analyst"}
      ],
      "service_account_ids": [12345, 67890]
    }'
  ```
</CodeGroup>

***

## Remove Service Account from Projects

<ParamField path="organizationId" type="integer" required>
  Your Mixpanel organization ID
</ParamField>

<ParamField body="projects" type="array" required>
  List of projects to remove from

  <Expandable title="projects[]">
    <ParamField body="id" type="integer">
      The project ID
    </ParamField>

    <ParamField body="service_account_ids" type="array">
      Service account IDs to remove from this project
    </ParamField>
  </Expandable>
</ParamField>

### Example Request

<CodeGroup>
  ```bash cURL theme={null}
  curl "https://mixpanel.com/api/app/organizations/123/service-accounts/remove-from-project" \
    -X POST \
    -u SERVICE_ACCOUNT_USERNAME:SERVICE_ACCOUNT_SECRET \
    -H "Content-Type: application/json" \
    -d '{
      "projects": [
        {"id": 456, "service_account_ids": [12345]},
        {"id": 789, "service_account_ids": [12345, 67890]}
      ]
    }'
  ```
</CodeGroup>

## Best Practices

<AccordionGroup>
  <Accordion title="Create service accounts for specific purposes">
    Use dedicated service accounts for different integrations:

    * `data-pipeline-bot`: For data exports
    * `ci-cd-bot`: For CI/CD integrations
    * `analytics-dashboard`: For custom dashboards
  </Accordion>

  <Accordion title="Use least privilege">
    Grant only the minimum required permissions:

    ```python theme={null}
    # Good: Specific role for specific purpose
    {
        "username": "readonly-reporter",
        "projects": [{"id": 123, "role": "consumer"}]
    }
    ```
  </Accordion>

  <Accordion title="Set expiration dates">
    Always set expiration dates for security:

    ```python theme={null}
    {
        "expires": "2025-12-31T23:59:59Z"
    }
    ```
  </Accordion>

  <Accordion title="Rotate credentials regularly">
    Rotate service account credentials periodically:

    ```python theme={null}
    # 1. Create new service account
    # 2. Update applications to use new credentials
    # 3. Delete old service account
    ```
  </Accordion>
</AccordionGroup>
