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

# Backend Implementation

This guide provides an overview of implementing secure tenant dashboard management using HMAC signature authentication for enterprise-grade multi-tenant security.

## Architecture Overview

```
Your API (Tenant Management)
    ↓
HMAC Signed Request → Papermap Dashboard API
    ↓
Dashboard Created/Retrieved
    ↓
Generate Iframe Token (HMAC Signed)
    ↓
Frontend Embeds Dashboard
```

### Key Components

1. **TenantDashboard Model** - Maps tenants to Papermap dashboards
2. **HMAC Signature Service** - Securely authenticates API requests
3. **Dashboard Handler** - Creates dashboards and generates embed tokens
4. **Dashboard Router** - API endpoints for dashboard operations

## Quick Start

Follow these guides in order to implement the backend:

<Steps>
  <Step title="Set Up Authentication">
    Implement HMAC signature authentication to securely communicate with the
    Papermap API.

    <Card title="HMAC Authentication" icon="key" href="/documentation/multi-tenancy/authentication">
      Learn how to implement HMAC-SHA256 signature authentication
    </Card>
  </Step>

  <Step title="Create Dashboards">
    Set up the ability to create and manage dashboards for your tenants.

    <Card title="Creating Dashboards" icon="table-columns" href="/documentation/multi-tenancy/creating-dashboards">
      Learn how to create dashboards via the Papermap API
    </Card>
  </Step>

  <Step title="Generate Embed Tokens">
    Generate secure tokens to embed dashboards in your frontend.

    <Card title="Iframe Embed Tokens" icon="code" href="/documentation/multi-tenancy/iframe-tokens">
      Learn how to generate secure embed tokens
    </Card>
  </Step>

  <Step title="Build API Endpoints">
    Create REST API endpoints for your frontend to interact with.

    <Card title="API Endpoints" icon="plug" href="/documentation/multi-tenancy/api-endpoints">
      Example REST API endpoints for dashboard operations
    </Card>
  </Step>

  <Step title="Secure Your Implementation">
    Follow security best practices for production deployment.

    <Card title="Security Best Practices" icon="shield-check" href="/documentation/multi-tenancy/security">
      Learn about security considerations and best practices
    </Card>
  </Step>
</Steps>

## Database Model

First, create a model to link your tenants to their Papermap dashboards:

<Info>
  It is not required to save it this way, but you need to have a way to map your tenant to their dashboards.
</Info>

<CodeGroup>
  ```python Python theme={null}
  from sqlalchemy import  Column, String

  from .database import Base
  class TenantDashboard(BaseModel):
      __tablename__ = 'tenant_dashboards'

      tenant_id = Column(String(255), nullable=False, index=True)
      workspace_id = Column(String(255), nullable=False)
      dashboard_id = Column(String(255), nullable=False)
  ```

  ```sql POSTGRESQL theme={null}
    CREATE TABLE tenant_dashboards (
      id SERIAL PRIMARY KEY,
      tenant_id VARCHAR(255) NOT NULL,
      workspace_id VARCHAR(255) NOT NULL,
      dashboard_id VARCHAR(255) NOT NULL,
      created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
      updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
  );

  CREATE INDEX idx_tenant_id ON tenant_dashboards (tenant_id);
  ```

  ```sql MYSQL theme={null}
  CREATE TABLE tenant_dashboards (
      id INT AUTO_INCREMENT PRIMARY KEY,
      tenant_id VARCHAR(255) NOT NULL,
      workspace_id VARCHAR(255) NOT NULL,
      dashboard_id VARCHAR(255) NOT NULL,
      created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
      updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
      INDEX idx_tenant_id (tenant_id)
  );
  ```
</CodeGroup>

**Purpose**: Links your tenant to a Papermap dashboard ID for secure access and isolation.

## Prerequisites

Before implementing, ensure you have:

* Papermap API credentials (API Key ID and Secret Key)
* Your workspace ID
* API endpoint URL (obtain from your Papermap dashboard settings)

<Warning>
  **Security Best Practices:** - Never hardcode API endpoints, keys, or secrets
  in your code - Always use environment variables or a secrets management system

  * The API endpoint URL should be treated as sensitive configuration - Store
    credentials securely and rotate them regularly
</Warning>

## Environment Configuration

Store your credentials securely in environment variables:

```bash .env theme={null}
PAPERMAP_API_KEY_ID=your-api-key-id
PAPERMAP_SECRET_KEY=your-secret-key-never-share
PAPERMAP_API_URL=<your-api-endpoint>
```

<Info>
  **Obtaining Your Configuration:** - **API Credentials**: Available in your
  Papermap dashboard under **Settings → API Keys** - **API Endpoint**: Available
  in **Settings → API Configuration** - Always use the values provided in your
  dashboard for your specific workspace
</Info>

## Implementation Guides

<CardGroup cols={2}>
  <Card title="HMAC Authentication" icon="key" href="/documentation/multi-tenancy/authentication">
    Implement secure HMAC signature authentication
  </Card>

  <Card title="Creating Dashboards" icon="table-columns" href="/documentation/multi-tenancy/creating-dashboards">
    Create and manage dashboards for tenants
  </Card>

  <Card title="Iframe Tokens" icon="code" href="/documentation/multi-tenancy/iframe-tokens">
    Generate secure embed tokens
  </Card>

  <Card title="API Endpoints" icon="plug" href="/documentation/multi-tenancy/api-endpoints">
    Build REST API endpoints
  </Card>

  <Card title="Tenant Usage" icon="chart-bar" href="/documentation/multi-tenancy/tenant-usage">
    Track per-tenant credit consumption
  </Card>

  <Card title="Security" icon="shield-check" href="/documentation/multi-tenancy/security">
    Security best practices
  </Card>

  <Card title="Best Practices" icon="star" href="/documentation/multi-tenancy/best-practices">
    Performance and scalability tips
  </Card>
</CardGroup>

## Common Pattern Reference

The HMAC signature pattern for tenant dashboards follows this flow:

```
1. Create Payload
   payload = workspace_id + valid_until

2. Generate Signature
   signature = HMAC-SHA256(payload, secret_key)

3. Send Request with Headers
   X-API-Key-ID: <api_key>
   X-Workspace-ID: <workspace_id>
   X-Valid-Until: <timestamp>
   X-Signature: <signature>

4. Store Result
   tenant_id → dashboard_id mapping

5. Generate Embed Token
   Encode {tenant_id, dashboard_id, signature, expires} as base64
```

This pattern ensures secure, time-limited access to tenant-specific dashboards.

## Next Steps

<Card title="Frontend Setup" icon="react" href="/documentation/multi-tenancy/setup">
  Learn how to embed dashboards in your frontend application
</Card>
