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

# Role-Based Access Control

> Control access to your Flexprice resources with role-based permissions

Flexprice's Role-Based Access Control (RBAC) system allows you to create service accounts with specific permissions for automated services and integrations. Instead of sharing your main account credentials, create dedicated service accounts with only the access they need.

## What is RBAC?

RBAC lets you create service accounts for your automated systems - like event ingestion services, analytics dashboards, or CI/CD pipelines - and assign each one specific roles that control what it can access.

**Key Benefits:**

* **Enhanced Security**: Limit what each service can access
* **Controlled Access**: Assign specific permissions based on what each service needs to do
* **Easy Management**: Create and revoke service account access without affecting your main account
* **Clear Separation**: Different API keys for different purposes

## How It Works

When you create a service account, you assign it one or more roles. Each role grants specific permissions to access certain resources.

**Example Flow:**

1. Create a service account and assign it the `event_ingestor` role
2. Generate an API key for that service account
3. The API key inherits the `event_ingestor` permissions
4. Your service can now ingest events but cannot access customer data or billing information

## User Types

### Regular Users (User Accounts)

Your standard user accounts for people. These accounts:

* Can log in to the dashboard
* Hold one of the organization-wide roles: `super_admin`, `all_writer`, or `all_reader`
* API keys from user accounts inherit the roles the user held when the key was created

A `super_admin` can change another user's roles at any time. See [Managing Roles](https://docs.flexprice.io/docs/rbac/manage-roles.md).

### Service Accounts

Automated accounts for your services and integrations. These accounts:

* Cannot log in to the dashboard
* Must have at least one role assigned
* Access Flexprice only through API keys
* API keys from service accounts have restricted access based on assigned roles
* Have fixed roles: a service account's roles cannot be changed after creation

> [!TIP]
>
> **Run automation on service accounts**
>
> Give scripts, integrations, and scheduled jobs their own service account and use its API key. A workflow tied to user's account stops working when their roles change or their account no longer exists.

## Available Roles

| Role | Account Type | Grants |
| - | - | - |
| `super_admin` | User and service account | Every action on every resource |
| `all_writer` | User account | Read and write on every resource |
| `all_reader` | User and service account | Read on every resource |
| `event_ingestor` | Service account | Send events |
| `event_reader` | Service account | Read events |

### super\_admin

For user accounts and service accounts that need unrestricted access to every resource.

**Can do:**

* Perform any action on any resource (read, write, delete)

**Cannot do:**

* Nothing. This role grants `*` permissions on `*` resources

**Use for:** Internal automation, migrations, or admin tooling that needs the same access as a user account. Treat the API key like a root credential.

> [!WARNING]
>
> Only assign `super_admin` when no narrower role works. Rotate keys frequently and store them in a secret manager.

### all\_writer

For people who need to read and write every resource.

**Can do:**

* Read every resource
* Write to every resource

**Cannot do:**

* Change any user's roles, which requires `super_admin`

**Use for:** People who create plans, manage customers, and issue invoices.

### all\_reader

For people and service accounts that need read-only access to every resource.

**Can do:**

* Read every resource

**Cannot do:**

* Create, update, or delete anything

**Use for:** Finance reviewers, support staff checking a customer's usage, reporting tools, data warehouse syncs, and monitoring jobs that must never write to Flexprice.

> [!NOTE]
>
> Assign `all_reader` to a service account to issue read-only API keys. See [Use Case 3](#use-case-3-read-only-integration).

### event\_ingestor

For services that send events to Flexprice.

**Can do:**

* Send events

**Cannot do:**

* Read events
* Read customers, subscriptions, invoices, payments, wallets, credit grants, or tasks
* Access any other resources

**Use for:** Event ingestion services, usage tracking tools

> [!NOTE]
>
> If the same service also reads wallet balances, invoices, or other billing data, give it a separate key with `all_reader` for those reads. See [Reading billing data](#reading-billing-data).

### event\_reader

For services that read events from Flexprice.

**Can do:**

* Read events

**Cannot do:**

* Send events
* Read customers, subscriptions, invoices, payments, wallets, credit grants, or tasks
* Modify any data

**Use for:** Analytics dashboards, reporting tools

## Reading billing data

Reads of the following resources require read permission, which `super_admin`, `all_writer`, and `all_reader` grant. A key with only `event_ingestor` or `event_reader` gets `403 Forbidden`.

| Resource | Examples |
| - | - |
| Customers | Get, list, search, lookup by external ID, usage, entitlements, upcoming grants, invoice summary |
| Subscriptions | Get, list, search, line items, schedules, entitlements, usage, change and modify previews |
| Invoices | Get, list, search, PDF download, invoice preview |
| Payments | Get, list |
| Wallets | Get, list, search, balance, transactions, customer wallets |
| Credit grants | Get, list |
| Tasks (imports and exports) | Get, list, download |

To read these resources from a service, create a service account with `all_reader` and use its key for reads. Keep the `event_ingestor` key for sending events.

## Real-World Use Cases

### Use Case 1: Event Ingestion Service

**Scenario**: You have a microservice that sends usage events to Flexprice.

**Solution**: Create a service account with the `event_ingestor` role and generate an API key.

**What happens**:

* ✅ Your service can send events successfully
* ❌ If it tries to read events, it gets 403 Forbidden
* ❌ If it tries to read customers, wallets, invoices, or other [billing data](#reading-billing-data), it gets 403 Forbidden

### Use Case 2: Analytics Dashboard

**Scenario**: Your analytics dashboard needs to read events for reporting.

**Solution**: Create a service account with the `event_reader` role and generate an API key.

**What happens**:

* ✅ Your dashboard can read event data
* ❌ If it tries to send events, it gets 403 Forbidden
* ❌ It cannot modify any data

### Use Case 3: Read-Only Integration

**Scenario**: A reporting tool or data warehouse sync needs to read customers, subscriptions, invoices, and usage, but must never change billing data.

**Solution**: Create a service account with the `all_reader` role and generate an API key. The key is read-only across every resource.

**What happens**:

* ✅ The integration can read every resource
* ❌ Any create, update, or delete request gets 403 Forbidden

## Validation Rules

> [!WARNING]
>
> **Service Accounts Require Roles**
>
> Every service account must have at least one role assigned. You cannot create a service account without specifying roles.

### Common Validation Errors

**Error: No Roles Assigned**

```json theme={null}
{
  "error": "Service accounts must have at least one role assigned"
}
```

**Error: Invalid Role**

```json theme={null}
{
  "error": "Invalid role: custom_role"
}
```

## Error Responses

### 403 Forbidden

When a service account tries to access a resource it doesn't have permission for:

```json theme={null}
{
  "message": "insufficient permissions"
}
```

**What this means**: The API key doesn't have the required permission. You need to:

1. Check which role the account behind the key has
2. Verify that role includes the permission you need
3. For a service account, create a replacement account with the role set you need, because service account roles are fixed at creation. For a user account, ask a `super_admin` to [update the user's roles](https://docs.flexprice.io/docs/rbac/manage-roles.md)
4. Generate a new API key (permissions are set when the key is created)

## Getting Started

### Step 1: Create a Service Account

Choose the appropriate role for your use case and create a service account:

```bash theme={null}
curl --request POST \
  --url https://api.cloud.flexprice.io/v1/users \
  --header 'Content-Type: application/json' \
  --header 'x-api-key: <api-key>' \
  --data '{
  "type": "service_account",
  "name": "<string>",
  "roles": [
    "<string>"
  ]
}'
```

### Step 2: Generate an API Key

Create an API key for your service account:

```bash theme={null}
curl --request POST \
  --url https://api.cloud.flexprice.io/v1/secrets/api/keys \
  --header 'Content-Type: application/json' \
  --header 'x-api-key: <api-key>' \
  --data '{
  "expires_at": "<string>",
  "name": "<string>",
  "service_account_id": "<string>",
  "type": "private_key"
}'
```

> [!WARNING]
>
> The API key is only shown once. Copy and store it securely immediately.

### Step 3: Use the API Key

Use the API key in your service:

```bash theme={null}
curl --request POST \
  --url https://api.cloud.flexprice.io/v1/events \
  --header 'Content-Type: application/json' \
  --header 'x-api-key: <api-key>' \
  --data '{
  "customer_id": "customer456",
  "event_id": "event123",
  "event_name": "api_request",
  "external_customer_id": "customer456",
  "properties": {
    "\"response_status\"": "200}",
    "{\"request_size\"": "100"
  },
  "source": "api",
  "timestamp": "2024-03-20T15:04:05Z"
}'
```

## Best Practices

> [!TIP]
>
> **Use Specific Roles**
>
> Assign only the roles your service needs. Don't use broader permissions than necessary.

> [!TIP]
>
> **Rotate API Keys Regularly**
>
> Generate new API keys periodically (recommended: every 90 days) and delete old ones.

> [!TIP]
>
> **Store Keys Securely**
>
> Use environment variables or secret management systems. Never commit API keys to your code repository.

## Troubleshooting

### Getting 403 Errors

**Symptom**: Your service is getting "Forbidden" errors when making API calls.

**Possible Causes**:

* The service account doesn't have the role that grants the required permission
* The API key was created before you assigned roles to the service account
* You're trying to access a resource that the assigned role doesn't cover

**Solution**:

1. Check what roles the service account has
2. Verify those roles include the permission you need
3. If you recently added roles, create a new API key
4. Test with a simple request to verify permissions

### Cannot Create Service Account

**Symptom**: Getting an error when trying to create a service account.

**Possible Causes**:

* Didn't specify any roles
* Specified an invalid role name

**Solution**: Make sure you're providing at least one valid role in the `roles` array.

## Next Steps

> [card] [Create Service Account](https://docs.flexprice.io/docs/rbac/service-accounts/create.md)
>
> Learn how to create service accounts →

> [card] [Managing Roles](https://docs.flexprice.io/docs/rbac/manage-roles.md)
>
> Change the roles of a user in your organization →

> [card] [Manage API Keys](https://docs.flexprice.io/docs/rbac/manage-api.md)
>
> Learn how to generate and manage API keys →


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.