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

# List loyalty rules and sections

> Render rule sections (rule groups) and their rules in your frontend using official APIs with pagination.

<head>
  <script type="application/ld+json">
    {JSON.stringify({
            "@context": "https://schema.org",
            "@graph": [
              {
                "@type": "TechArticle",
                "headline": "List loyalty rules and sections",
                "description": "Render rule sections (rule groups) and their rules in your frontend using official APIs with pagination.",
                "author": {"@type": "Organization", "name": "Snag Solutions", "url": "https://www.snagsolutions.io/"},
                "publisher": {"@type": "Organization", "name": "Snag Solutions", "url": "https://www.snagsolutions.io/", "logo": {"@type": "ImageObject", "url": "https://assets.snagsolutions.io/public/docs/snag-logo-dark-no-bg.svg"}},
                "mainEntityOfPage": "https://docs.snagsolutions.io/loyalty/development/list-loyalty-rules"
              },
              {
                "@type": "BreadcrumbList",
                "itemListElement": [
                  {"@type": "ListItem", "position": 1, "name": "Home", "item": "https://docs.snagsolutions.io/welcome"},
                  {"@type": "ListItem", "position": 2, "name": "Loyalty", "item": "https://docs.snagsolutions.io/loyalty/loyalty-overview"},
                  {"@type": "ListItem", "position": 3, "name": "Development", "item": "https://docs.snagsolutions.io/loyalty/development/getting-started"},
                  {"@type": "ListItem", "position": 4, "name": "List loyalty rules"}
                ]
              }
            ]
          })}
  </script>
</head>

<Info>
  This guide shows you how to list loyalty rule sections (rule groups) and the
  rules under each section in your frontend. For all parameters, filters, and
  full response schemas, see the API references: -{' '}

  <a href="/api-reference/loyalty/get-loyalty-rule-groups" target="_blank" rel="noopener">
    Get Loyalty Rule Groups
  </a>

  -{' '}

  <a href="/api-reference/loyalty/get-loyalty-rules" target="_blank" rel="noopener">
    Get Loyalty Rules
  </a>
</Info>

## Prerequisites

* You can make authenticated requests with the `X-API-KEY` header.
* You have your base URL (e.g., `$BASE_URL`).

## How it works (at a glance)

* Fetch sections via Rule Groups.
* For each section, fetch its rules.
* Use cursor-style pagination with `startingAfter` and stop when `hasNextPage` is false.

<Steps>
  <Step title="Fetch rule groups (sections)">
    <RequestExample>
      ```bash cURL theme={null}
      curl -X GET "$BASE_URL/api/loyalty/rule-groups?limit=20" \
        -H "X-API-KEY: $API_KEY"
      ```
    </RequestExample>

    <Note>
      See full request/response details: <a href="/api-reference/loyalty/get-loyalty-rule-groups" target="_blank" rel="noopener">Get Loyalty Rule Groups</a>.
    </Note>
  </Step>

  <Step title="Fetch rules for a given section">
    <RequestExample>
      ```bash cURL theme={null}
      curl -X GET "$BASE_URL/api/loyalty/rules?loyaltyRuleGroupId=<GROUP_ID>&limit=20" \
        -H "X-API-KEY: $API_KEY"
      ```
    </RequestExample>

    <Note>
      See full request/response details: <a href="/api-reference/loyalty/get-loyalty-rules" target="_blank" rel="noopener">Get Loyalty Rules</a>.
    </Note>
  </Step>

  <Step title="Render in your frontend with minimal fetchers">
    <CodeGroup>
      ```typescript React theme={null}
      // Minimal fetchers (client-side or server-side)
      async function listRuleGroups(baseUrl: string, apiKey: string, startingAfter?: string) {
        const url = new URL('/api/loyalty/rule-groups', baseUrl);
        url.searchParams.set('limit', '20');
        if (startingAfter) url.searchParams.set('startingAfter', startingAfter);

        const res = await fetch(url.toString(), { headers: { 'X-API-KEY': apiKey } });
        if (!res.ok) throw new Error(`rule-groups failed: ${res.status}`);
        return res.json() as Promise<{ data: Array<{ id: string; name: string; slug?: string; description?: string }>; hasNextPage: boolean }>;
      }

      async function listRulesForGroup(baseUrl: string, apiKey: string, groupId: string, startingAfter?: string) {
        const url = new URL('/api/loyalty/rules', baseUrl);
        url.searchParams.set('limit', '20');
        url.searchParams.set('loyaltyRuleGroupId', groupId);
        if (startingAfter) url.searchParams.set('startingAfter', startingAfter);
        
        const res = await fetch(url.toString(), { headers: { 'X-API-KEY': apiKey } });
        if (!res.ok) throw new Error(`rules failed: ${res.status}`);
        return res.json() as Promise<{ data: Array<{ id: string; name: string; description?: string }>; hasNextPage: boolean }>;
      }

      // Example usage:
      // 1) const { data: groups, hasNextPage } = await listRuleGroups(BASE_URL, API_KEY);
      // 2) For each group.id, call listRulesForGroup(BASE_URL, API_KEY, group.id)
      // 3) Append results while hasNextPage is true (see next step)

      ```
    </CodeGroup>
  </Step>
</Steps>

<Check>
  You can see rule sections rendered and, on expanding a section, the rules
  under it. Additional pages load until `hasNextPage` is false.
</Check>

## Next Steps

Now that you can list loyalty rules and sections, you're ready to:

<Card title="Create Your First Rule" icon="star" href="/loyalty/create-your-first-rule">
  Learn how to create and configure loyalty rules for your program.
</Card>

<Card title="Rule Configuration" icon="gear" href="/loyalty/rules-configuration">
  Understand how to configure rule parameters and settings.
</Card>
