Skip to main content
GET
/
api
/
loyalty
/
config
Loyalty Config
curl --request GET \
  --url https://api.example.com/api/loyalty/config
{
  "tiers": [
    {
      "id": "<string>",
      "name": "<string>",
      "rank": 123,
      "color": "<string>",
      "icon": "<string>"
    }
  ],
  "rules": [
    {
      "id": "<string>",
      "location_id": "<string>",
      "tier_id": "<string>",
      "min_spend": 123,
      "min_visits": 123
    }
  ],
  "benefits": [
    {
      "id": "<string>",
      "tier_id": "<string>",
      "title": "<string>",
      "description": "<string>",
      "is_active": true
    }
  ],
  "locations": [
    {
      "id": "<string>",
      "name": "<string>"
    }
  ],
  "stats": {
    "total": 123,
    "byTier": {}
  }
}

Loyalty Config

Retrieve complete loyalty program configuration for the authenticated user’s organization, including tiers, rules, benefits, and statistics.

Endpoint

GET /api/loyalty/config

Authentication

Requires authenticated user session.

Response

tiers
array
Array of loyalty tiers (global, sorted by rank)
rules
array
Loyalty rules for the organization’s locations
benefits
array
Active loyalty benefits
locations
array
Organization’s locations
stats
object
Customer statistics for the organization

Example Request

curl -X GET https://your-domain.com/api/loyalty/config \
  -H "Cookie: your-session-cookie"

Example Response

{
  "tiers": [
    {
      "id": "tier-bronce-uuid",
      "name": "Bronce",
      "rank": 1,
      "color": "#CD7F32",
      "icon": "bronze-medal"
    },
    {
      "id": "tier-plata-uuid",
      "name": "Plata",
      "rank": 2,
      "color": "#C0C0C0",
      "icon": "silver-medal"
    },
    {
      "id": "tier-oro-uuid",
      "name": "Oro",
      "rank": 3,
      "color": "#FFD700",
      "icon": "gold-medal"
    },
    {
      "id": "tier-vip-uuid",
      "name": "VIP",
      "rank": 4,
      "color": "#9333EA",
      "icon": "crown"
    }
  ],
  "rules": [
    {
      "id": "rule-uuid-1",
      "location_id": "location-madrid-centro",
      "tier_id": "tier-plata-uuid",
      "min_spend": 500,
      "min_visits": 5
    },
    {
      "id": "rule-uuid-2",
      "location_id": "location-madrid-centro",
      "tier_id": "tier-oro-uuid",
      "min_spend": 1500,
      "min_visits": 12
    }
  ],
  "benefits": [
    {
      "id": "benefit-uuid-1",
      "tier_id": "tier-vip-uuid",
      "title": "10% de descuento",
      "description": "En todas las consumiciones",
      "is_active": true
    }
  ],
  "locations": [
    {
      "id": "location-madrid-centro",
      "name": "La Tasca Madrid · Centro"
    },
    {
      "id": "location-madrid-salamanca",
      "name": "La Tasca Madrid · Salamanca"
    }
  ],
  "stats": {
    "total": 265,
    "byTier": {
      "bronce": 150,
      "plata": 75,
      "oro": 30,
      "vip": 10
    }
  }
}

Error Responses

401 Unauthorized

{
  "error": "No autorizado"
}

400 Bad Request

{
  "error": "Sin organización"
}

500 Internal Server Error

{
  "error": "Database error message"
}

Implementation Details

Data Scoping

The endpoint automatically scopes data to the user’s organization:
  1. Get User Profile - Retrieve organization_id from authenticated user
  2. Fetch Tiers - Global tiers (not organization-specific)
  3. Fetch Locations - Filtered by organization_id
  4. Fetch Rules - Filtered by location IDs from step 3
  5. Fetch Benefits - Global active benefits
  6. Calculate Stats - Customer counts grouped by tier

Source Code Reference

app/api/loyalty/config/route.ts
import { createClient } from "@/lib/supabase/server";

export async function GET() {
    const supabase = await createClient();
    const { data: { user } } = await supabase.auth.getUser();
    
    if (!user) {
        return NextResponse.json({ error: "No autorizado" }, { status: 401 });
    }

    const { data: profile } = await supabase
        .from('profiles')
        .select('organization_id')
        .eq('id', user.id)
        .single();

    // Fetch tiers, rules, benefits, locations, and stats
    // ...
}

Use Cases

  • Display loyalty program configuration in admin dashboard
  • Build loyalty tier UI components
  • Configure location-specific tier requirements
  • Show customer distribution across tiers
  • Validate loyalty rules before updates
Tiers and benefits are global (shared across all organizations), while rules are location-specific.

Build docs developers (and LLMs) love