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

# 面向返现和联盟网站的商户数据源

> 一个脚本即可拉取所有 Fluz 商户、其返现比例以及您的推荐链接，可直接在您的网站上展示。

如果您运营返现、优惠券或折扣网站，只需阅读本页即可展示 Fluz 商户。您只需复制四个值并运行一个脚本，就能得到一个文件，其中包含每个商户、其返现比例，以及一个会将新会员记入您名下的链接。

该脚本适用于个人和企业 Fluz 账户。每天运行一次即可。

## 您将获得什么

每个商户对应一条记录：

```json theme={null}
{
  "name": "Kohl's",
  "slug": "kohls",
  "cashback": 6,
  "logo_url": "https://storage.googleapis.com/.../kohls-logo.jpg",
  "categories": ["Retail"],
  "link": "https://fluz.app/store/kohls?referred_by=YOURCODE"
}
```

| 字段 | 说明 |
| :- | :- |
| `name` | 在您网站上显示的商户名称 |
| `slug` | Fluz 为商户分配的 ID。它不会改变，因此您可以每天匹配商户。 |
| `cashback` | Fluz 在该商户提供的最高返现比例，以百分比表示。`6` 表示最高 6%。 |
| `logo_url` | 商户标志，或 `null` |
| `categories` | 购物类别，例如餐饮或旅行。可以为空。 |
| `link` | 该商户在 fluz.app 上的页面，附带您的推荐码。请将其用作外部链接。 |

## 开始之前

您需要四个值，全部来自您的 Fluz 账户。

| 值 | 获取位置 |
| :- | :- |
| **API Key** | 登录 [fluz.app/for-developers](https://fluz.app/for-developers)，创建一个应用并打开它。 |
| **User ID** | 与 API Key 位于同一页面 |
| **Account ID** | 与 API Key 位于同一页面 |
| **推荐码** | 在 fluz.app 上，打开 **Maximize** > **Invite a friend**，然后选择 **Copy**。 |

> [!WARNING]
>
> 请在 **fluz.app/for-developers** 创建应用。在沙盒（`uni.staging.fluzapp.com`）中创建的应用使用不同的密钥，正式 API 会拒绝该密钥并返回 `Error verifying API basic token`。

## 运行数据源

> [tab] PHP
>
> 将以下内容保存为 `fluz-feed.php`，在顶部粘贴您的四个值，然后运行 `php fluz-feed.php`。它会在脚本所在目录写入 `fluz-feed.json`。需要 PHP 7.4 或更高版本，并启用 curl 扩展。
>
> ```php fluz-feed.php theme={null}
> <?php
> // Fluz merchant feed: every Fluz merchant with its cashback rate and your referral link.
> // Run it once a day, for example from cron. Needs PHP 7.4 or newer with the curl extension.
>
> $API_KEY       = 'YOUR_API_KEY';        // fluz.app/for-developers > your app > API Key
> $USER_ID       = 'YOUR_USER_ID';        // same page
> $ACCOUNT_ID    = 'YOUR_ACCOUNT_ID';     // same page
> $REFERRAL_CODE = 'YOUR_REFERRAL_CODE';  // fluz.app > Maximize > Invite a friend
>
> const FLUZ_API = 'https://transactional-graph.fluzapp.com/api/v1/graphql';
>
> function fluz($auth, $query, $variables = [])
> {
>     $ch = curl_init(FLUZ_API);
>     curl_setopt_array($ch, [
>         CURLOPT_POST           => true,
>         CURLOPT_RETURNTRANSFER => true,
>         CURLOPT_TIMEOUT        => 60,
>         CURLOPT_HTTPHEADER     => ['Authorization: ' . $auth, 'Content-Type: application/json'],
>         CURLOPT_POSTFIELDS     => json_encode(['query' => $query, 'variables' => $variables]),
>     ]);
>     $body   = curl_exec($ch);
>     $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
>     if ($body === false) {
>         throw new Exception('Request failed: ' . curl_error($ch));
>     }
>     curl_close($ch);
>     $json = json_decode($body, true);
>     if ($status !== 200 || !empty($json['errors'])) {
>         throw new Exception("Fluz API error (HTTP $status): $body");
>     }
>     return $json['data'];
> }
>
> // 1. Get an access token with your API key.
> $data = fluz('Basic ' . $API_KEY,
>     'mutation ($userId: UUID, $accountId: UUID, $scopes: [ScopeType!]!) {
>        generateUserAccessToken(userId: $userId, accountId: $accountId, scopes: $scopes) { token }
>      }',
>     ['userId' => $USER_ID, 'accountId' => $ACCOUNT_ID, 'scopes' => ['LIST_OFFERS']]
> );
> $token = $data['generateUserAccessToken']['token'];
>
> // 2. Read the catalog 20 merchants at a time until a page comes back empty.
> $query = 'query ($paginate: OffsetInput, $offerTypes: OfferTypesInput) {
>   getMerchants(paginate: $paginate, offerTypes: $offerTypes) {
>     name
>     slug
>     logoUrl
>     categories { name }
>     offers { offerRates { maxUserRewardValue } }
>   }
> }';
>
> $feed = [];
> for ($offset = 0; ; $offset += 20) {
>     $page = fluz('Bearer ' . $token, $query, [
>         'paginate'   => ['limit' => 20, 'offset' => $offset],
>         'offerTypes' => ['giftCardOffer' => true, 'cardLinkedOffer' => false],
>     ])['getMerchants'];
>
>     if (empty($page)) {
>         break;
>     }
>
>     foreach ($page as $m) {
>         // A merchant can have more than one offer. List its best rate.
>         $rate = 0;
>         foreach ($m['offers'] ?? [] as $offer) {
>             foreach ($offer['offerRates'] ?? [] as $r) {
>                 $rate = max($rate, (float) $r['maxUserRewardValue']);
>             }
>         }
>
>         $feed[] = [
>             'name'       => $m['name'],
>             'slug'       => $m['slug'],
>             'cashback'   => $rate,
>             'logo_url'   => $m['logoUrl'],
>             'categories' => array_column($m['categories'] ?? [], 'name'),
>             'link'       => 'https://fluz.app/store/' . $m['slug'] . '?referred_by=' . rawurlencode($REFERRAL_CODE),
>         ];
>     }
> }
>
> // 3. Save the feed. Your site reads this file.
> file_put_contents(__DIR__ . '/fluz-feed.json', json_encode($feed, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
> echo count($feed) . " merchants saved to fluz-feed.json\n";
> ```

> [tab] curl
>
> 您可以在任何语言中使用这两个请求。两者使用相同的 URL：
>
> ```text theme={null}
> POST https://transactional-graph.fluzapp.com/api/v1/graphql
> ```
>
> **1. 获取访问令牌。** 请原样发送控制台中显示的 API Key，它已经过编码。
>
> ```bash theme={null}
> curl -X POST https://transactional-graph.fluzapp.com/api/v1/graphql \
>   -H "Authorization: Basic YOUR_API_KEY" \
>   -H "Content-Type: application/json" \
>   -d '{
>     "query": "mutation ($userId: UUID, $accountId: UUID, $scopes: [ScopeType!]!) { generateUserAccessToken(userId: $userId, accountId: $accountId, scopes: $scopes) { token } }",
>     "variables": { "userId": "YOUR_USER_ID", "accountId": "YOUR_ACCOUNT_ID", "scopes": ["LIST_OFFERS"] }
>   }'
> ```
>
> 从响应中复制 `data.generateUserAccessToken.token`。
>
> **2. 获取商户，每次 20 个。** 从 `offset` 为 0 开始，每次加 20 并重复请求，直到 `getMerchants` 返回空列表 `[]`。
>
> ```bash theme={null}
> curl -X POST https://transactional-graph.fluzapp.com/api/v1/graphql \
>   -H "Authorization: Bearer ACCESS_TOKEN" \
>   -H "Content-Type: application/json" \
>   -d '{
>     "query": "query ($paginate: OffsetInput, $offerTypes: OfferTypesInput) { getMerchants(paginate: $paginate, offerTypes: $offerTypes) { name slug logoUrl categories { name } offers { offerRates { maxUserRewardValue } } } }",
>     "variables": { "paginate": { "limit": 20, "offset": 0 }, "offerTypes": { "giftCardOffer": true, "cardLinkedOffer": false } }
>   }'
> ```
>
> 对于每个商户，将最高的 `maxUserRewardValue` 作为其返现比例，并构建其链接：
>
> ```text theme={null}
> https://fluz.app/store/{slug}?referred_by={your referral code}
> ```

## 验证

1. 打开 `fluz-feed.json`，检查其中是否列出了带有比例的商户。
2. 在隐私浏览窗口中打开某个 `link`。它应该跳转到该商户在 fluz.app 上的页面。
3. 检查链接末尾的代码是否与您的推荐码完全一致，包括大小写。错误的代码不会显示任何错误，但注册不会记入您名下。

## 保持数据最新

* 每天运行一次脚本，例如通过 cron。Fluz 每天更新两次目录，返现比例经常变化。
* 访问令牌的有效期很短。脚本每次运行都会获取新的令牌，因此您无需管理它。
* 返现比例变化时，商户 slug 不会改变，因此您已发布的链接仍然有效。

## 如果无法正常工作

> [accordion] Error verifying API basic token
>
> API Key 未被接受。请检查：
>
> * 您是在 **fluz.app/for-developers** 创建的应用，而不是在沙盒中。
> * 您已完整粘贴 API Key，与显示的内容完全一致。请勿再次编码。
> * 密钥没有被重新生成。重新生成的密钥会立即取代旧密钥。
> * User ID 和 Account ID 与密钥来自同一个应用。

> [accordion] 数据源中有商户，但比例为 0
>
> 某些商户目前没有返现比例。您可以在网站上跳过它们，或不显示比例而直接展示。当比例恢复时，它们会重新出现在数据源中。

> [accordion] 通过我的链接注册的用户没有记入我名下
>
> 只有当对方在打开您链接的同一浏览器中创建**新的** Fluz 账户时，才会记入积分。请检查链接中的推荐码是否与您在 Fluz 中的推荐码完全一致。请参阅[推荐积分的运作方式](https://docs.fluz.app/formatting-referral-links.md#how-referral-credit-works)。

> [accordion] 脚本运行到一半就停止
>
> 脚本遇到任何错误都会停止，并打印 Fluz 返回的响应。请重新运行。如果相同的错误反复出现，请将该消息发送给您的 Fluz 联系人。

## 更多详情

* [推荐链接](https://docs.fluz.app/formatting-referral-links.md)：所有链接格式，以及推荐积分的运作方式
* [获取目录](https://docs.fluz.app/get-gift-card-merchant-catalog.md)：`getMerchants` 的所有字段和筛选条件
* [API 凭证](https://docs.fluz.app/get-started/api-credentials.md)：访问令牌的运作方式


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