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

> ## Agent Instructions
> Test in the sandbox first: base URL https://api-sandbox.y.uno (Test Mode in the Dashboard). Production is https://api.y.uno (Live Mode).
> Every API request needs the public-api-key and private-secret-key headers. Get them in the Dashboard at https://dashboard.y.uno/developers (Developers > Credentials); sandbox and production keys are different.
> Send an X-Idempotency-Key (a UUID) when creating payments, refunds and cancels, so retries do not create duplicates.
> Start with the Quickstart (https://docs.y.uno/docs/quickstart.md) and the agent guide (https://docs.y.uno/agents.md). To search these docs from a coding agent, connect the docs MCP server at https://docs.y.uno/mcp.

# 获取路由推荐

> 发送支付上下文和您的候选服务提供商，Yuno 会根据您自己上报的交易历史返回排名后的推荐路由。

此 endpoint 根据您通过[交易上报 API](https://docs.y.uno/zh-hans/reference/reporting/report-transactions.md) 发送给 Yuno 的结果历史，推荐应将支付路由到**您自己的哪个服务提供商账户**。您发送支付上下文以及您实际可用的候选项；Yuno 返回推荐的候选项、包含每个候选项指标的完整排名，以及一个 `recommendation_id`。

交易本身从不经过 Yuno。**Yuno 负责推荐，您负责决策和处理。**

> [!NOTE]
>
> **按组织启用**：此 API 需要为您的组织激活。请联系您的大客户经理（KAM）启用。在此之前，每次调用都会返回 `403 PRODUCT_NOT_ENABLED`。

## 工作原理

1. 您通过[交易上报 API](https://docs.y.uno/zh-hans/reference/reporting/report-transactions.md) 上报在 Yuno 之外处理的交易结果。只有 `origin: REPORTED` 的支付会用于统计；Yuno 处理的流量属于不同的群体，因此被有意排除在外。
2. 在路由支付之前，您使用支付上下文和候选服务提供商调用此 endpoint。
3. Yuno 基于您上报的支付在滚动 **30 天**窗口内计算每个候选项的统计数据（批准率、服务提供商平均延迟、样本量），按服务提供商、国家和支付方式细分；发送卡片 BIN 时还会按 BIN 细分（请参阅 [BIN 级统计](#bin-level-statistics)），发送 `CIT`/`MIT` 发起方式时还会按发起方式细分（请参阅[发起方式级统计](#initiation-level-statistics)），然后返回按您的目标排序的排名。
4. 您自行路由该支付，并使用此处返回的 `recommendation_id` 上报其结果，Yuno 会将结果归因到这条确切的推荐。如果上报的事件不包含 `recommendation_id`，Yuno 会回退为使用您在此处发送的同一个 `merchant_order_id`。

只有处于终态的上报支付才会计入批准率：`SUCCEEDED` 计为已批准，`SUCCEEDED + DECLINED` 计为已决定。处于任何其他状态（例如 `ERROR`、待处理）的支付不计入批准率和延迟，但仍会计入 `sample_size`。

## 请求体

**account\_id** `string (UUID)` *required*

推荐所针对的商户账户。在请求体中传递；Yuno 会在内部将其提升为账户标头。必须属于 API 凭证所在的组织。

**merchant\_order\_id** `string`

可选，由商户提供的订单参考（最多 255 个字符）。与生成的推荐一起存储，作为闭环归因的关联键。

**payment** `object` *required*

用于对推荐进行评分的支付上下文。

**属性**

**amount** `object` *required*

**属性**

**currency** `string` *required*

ISO 4217 币种代码，例如 `USD`。

**value** `number` *required*

交易金额。必须 `≥ 0`。

**country** `string` *required*

ISO 3166-1 alpha-2 国家代码，例如 `US`。用于选择统计细分。

**payment\_method** `object` *required*

**属性**

**type** `string` *required*

例如 `CARD`、`PIX`、`WALLET`。用于选择统计细分。

**details** `object`

可选。`card.bin`：卡片的 BIN，必须恰好为 **6 位或 8 位数字**。当该 BIN 有足够的上报历史时，排名会根据其自身的统计数据评分（请参阅 [BIN 级统计](#bin-level-statistics)）；否则，或对于任何其他值，统计数据会回退到国家和支付方式。

**initiation** `enum`

可选。`CIT`（持卡人发起）或 `MIT`（商户发起）。当您发送此字段，且相同发起方式在该细分中有足够的上报历史时，排名会根据该发起方式自身的统计数据评分（请参阅[发起方式级统计](#initiation-level-statistics)）；否则回退到整个细分。省略此字段时，排名将完全按以前的方式基于整个细分进行。

**candidates** `object[]` *required*

要排名的服务提供商。包含 **2 到 20** 项；只有单个候选项的推荐没有意义。

**项**

**provider\_id** `string` *required*

服务提供商代码，例如 `ADYEN`、`STRIPE`、`DLOCAL`。

**merchant\_connection\_id** `string`

可选的透传字段，供您自己记账使用，按发送时的原样返回，不做验证。

**optimize\_for** `enum` (default: APPROVAL\_RATE)

排名所优化的目标。`APPROVAL_RATE`（默认）、`LATENCY` 或 `BALANCED`。

## 响应

**recommendation\_id** `string`

所生成推荐的唯一标识符，为小写的 UUID v4。每次调用都会生成新的 id。在[上报事件](https://docs.y.uno/zh-hans/reference/reporting/report-transactions.md#recommendation-id-for-outcome-attribution)中将其作为 `recommendation_id` 发回，即可将结果归因到这条推荐。

**merchant\_order\_id** `string`

提供时为请求字段的回显；否则为 `null`。

**optimized\_for** `enum`

应用于排名的目标。

**decision\_source** `enum`

排名的决定方式：`MERCHANT_HISTORY` 或 `INSUFFICIENT_DATA`（见下文）。

**recommended** `object`

排名的第一位。始终存在。

**属性**

**provider\_id** `string`

推荐的服务提供商代码。

**merchant\_connection\_id** `string`

候选项的 `merchant_connection_id`（如已提供）。

**ranking** `object[]`

所有候选项，按目标排序。

**项**

**provider\_id** `string`

服务提供商代码。

**merchant\_connection\_id** `string`

候选项的 `merchant_connection_id`（如已提供）。

**approval\_rate** `number`

基于上报历史计算的 `SUCCEEDED / (SUCCEEDED + DECLINED)`，取值 `0` 到 `1`。没有数据时为 `null`。

**avg\_latency\_ms** `integer`

基于 `SUCCEEDED + DECLINED` 计算的平均授权延迟（毫秒）。没有数据时为 `null`。

**sample\_size** `integer`

窗口期内该细分中所有状态的上报支付总数，表示有多少历史数据支撑此候选项的指标。

### 解读响应

* **`recommended` 始终存在**，即使没有足够的历史数据也是如此。您无需编写特殊分支：`decision_source` 会告诉您它的可信程度。
* **`decision_source: MERCHANT_HISTORY`**：至少有一个候选项达到了最小样本量；排名反映的是基于您上报支付的真实统计数据。
* **`decision_source: INSUFFICIENT_DATA`**：没有候选项达到近期上报事件的最小样本量。完整排名仍会返回，推荐会回退到可用的最佳数据，最后才回退到请求中的顺序。在依赖该顺序之前，请查看每个候选项的 `sample_size`，了解有多少历史数据支撑它。
* **`sample_size`** 体现了推荐的可信度。基于 12 个事件的高批准率与基于 12,000 个事件的高批准率并不是同样的信号。
* **始终返回完整的 `ranking`**，按您的目标排序。如果推荐的候选项拒绝了支付，请使用下一个条目，无需再次调用即可进行级联重试。

## BIN 级统计

如果您在 `CARD` 事件中上报卡片 BIN（[交易上报 API](https://docs.y.uno/zh-hans/reference/reporting/report-transactions.md) 中的 `payment_method_details.card.bin`），请在此处的 `payment.payment_method.details.card.bin` 中发送相同的 BIN。对于每个候选项，排名会在该支付所在国家中，使用达到最小样本量的最具体细分：

1. **8 位 BIN**：当您发送 8 位数字且该 BIN 有足够的上报历史时。
2. **6 位 BIN**：前 6 位数字（也是您发送 6 位 BIN 时使用的细分）。
3. **国家**：服务提供商、国家和支付方式，与不发送 BIN 时相同。
4. **支付方式**：跨国家的服务提供商和支付方式。

没有上报历史的 BIN 会直接回退到国家和支付方式细分：响应与不发送 BIN 时完全相同。不是恰好 6 位或 8 位数字的值会被忽略，而不会被拒绝。

## 发起方式级统计

如果您在事件中上报 `initiation`（`CIT` 或 `MIT`）（在[交易上报 API](https://docs.y.uno/zh-hans/reference/reporting/report-transactions.md) 中），请在此处发送相同的 `payment.initiation`。不同服务提供商对 CIT 和 MIT 的批准情况差异很大，因此在上述每个细分级别，排名会先尝试使用**相同发起方式**的历史数据，只有在其未达到最小样本量时才使用整个级别。实际顺序为：BIN8 + 发起方式 → BIN8 → BIN6 + 发起方式 → BIN6 → 国家 + 发起方式 → 国家 → 支付方式 + 发起方式 → 支付方式。

未包含发起方式的上报事件只计入不区分发起方式（整个级别）的统计。**不带** `payment.initiation` 的请求会基于整个细分评分，与使用此字段之前完全相同，响应不变。`approval_rate`、`avg_latency_ms` 和 `sample_size` 始终反映实际使用的细分。

## 选择目标

`optimize_for` 决定排名的排序方式：`APPROVAL_RATE`（默认）、`LATENCY` 或 `BALANCED`（两者的加权组合）。目前还没有成本目标：Reporting API 不收集费用，Yuno 也不会基于没有的数据进行排名。

`LATENCY` 和 `BALANCED` 基于您在每个事件中上报的 `latency_ms` 进行排名。如果您上报的事件不包含 `latency_ms`，就没有可用于排名的延迟数据：`LATENCY` 会按请求中的顺序返回候选项，`BALANCED` 则只考虑批准率。要使用这两个目标中的任一个，请上报 `latency_ms`（您测得的服务提供商往返时间）。

## 候选项是您的约束层

推荐结果始终是您发送的 `candidates` 之一：Yuno 从不推荐您未列出的服务提供商。请只发送此支付确实允许使用的服务提供商（资格、合同、承诺交易量），推荐结果自然会遵守这些约束。

## 重试

此 endpoint 没有幂等性标头，也不需要：每次调用都会计算新的推荐并生成新的 `recommendation_id`，请求推荐不会改变任何状态。网络故障后重试始终是安全的。

## 示例

```bash cURL theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
curl -X POST 'https://api-sandbox.y.uno/v1/routing/recommendations' \
  -H 'public-api-key: <YOUR_PUBLIC_KEY>' \
  -H 'private-secret-key: <YOUR_SECRET_KEY>' \
  -H 'Content-Type: application/json' \
  -d '{
    "account_id": "7825fc5e-e50a-4248-9ae8-5d3786afb0be",
    "merchant_order_id": "order_45678",
    "payment": {
      "amount": { "currency": "USD", "value": 99.00 },
      "country": "US",
      "payment_method": { "type": "CARD", "details": { "card": { "bin": "45717360" } } }
    },
    "candidates": [
      { "provider_id": "ADYEN", "merchant_connection_id": "acct-adyen-us-1" },
      { "provider_id": "STRIPE" },
      { "provider_id": "DLOCAL" }
    ],
    "optimize_for": "APPROVAL_RATE"
  }'
```

```json 200 — MERCHANT_HISTORY theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
{
  "recommendation_id": "3e37f0c9-e9cd-4d85-9e1a-ec5f09af296d",
  "merchant_order_id": "order_45678",
  "optimized_for": "APPROVAL_RATE",
  "decision_source": "MERCHANT_HISTORY",
  "recommended": { "provider_id": "ADYEN", "merchant_connection_id": "acct-adyen-us-1" },
  "ranking": [
    { "provider_id": "ADYEN",  "merchant_connection_id": "acct-adyen-us-1", "approval_rate": 0.9987, "avg_latency_ms": 180, "sample_size": 767 },
    { "provider_id": "STRIPE", "merchant_connection_id": null, "approval_rate": 0.9167, "avg_latency_ms": 150, "sample_size": 60 },
    { "provider_id": "DLOCAL", "merchant_connection_id": null, "approval_rate": 0.8000, "avg_latency_ms": 210, "sample_size": 60 }
  ]
}
```

```json 200 — INSUFFICIENT_DATA theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
{
  "recommendation_id": "7249c858-1f0f-4654-93e7-ebe34bc5048f",
  "merchant_order_id": "order_45678",
  "optimized_for": "APPROVAL_RATE",
  "decision_source": "INSUFFICIENT_DATA",
  "recommended": { "provider_id": "ADYEN", "merchant_connection_id": null },
  "ranking": [
    { "provider_id": "ADYEN",  "merchant_connection_id": null, "approval_rate": null, "avg_latency_ms": null, "sample_size": 0 },
    { "provider_id": "STRIPE", "merchant_connection_id": null, "approval_rate": null, "avg_latency_ms": null, "sample_size": 0 }
  ]
}
```

```json 400 — Too Few Candidates theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
{
  "code": "INVALID_PARAMETERS",
  "messages": ["The field 'candidates' has a value less than allowed."]
}
```

## 错误

错误会返回一个 `code` 和一个 `messages` 数组。

| HTTP | `code` | 触发情况 |
| - | - | - |
| `400` | `INVALID_PARAMETERS` | 请求体格式错误、缺少必填字段、候选项少于 2 个或多于 20 个、`country`/`currency` 不符合 ISO 标准，或枚举值无效（`optimize_for`、`initiation`）。 |
| `400` | `INVALID_ACCOUNT_ID` | `account_id` 格式错误或不属于您的组织。 |
| `401` | `INVALID_CREDENTIALS` | 缺少 `PUBLIC-API-KEY` / `PRIVATE-SECRET-KEY` 标头或标头无效。 |
| `403` | `PRODUCT_NOT_ENABLED` | 您的组织未启用 Routing Recommendations API。请联系您的 KAM。 |
| `502` | `ROUTING_UPSTREAM_ERROR` | 无法连接路由服务。 |


## OpenAPI

````yaml openapi/zh-hans/organizations/routing/get-routing-recommendations.json POST /routing/recommendations
openapi: 3.1.0
info:
  title: Routing API - Recommendations
  version: 1.0.0
servers:
  - url: https://api-sandbox.y.uno/v1
    description: Sandbox
  - url: https://api.y.uno/v1
    description: Production (US)
security:
  - sec0: []
    sec1: []
paths:
  /routing/recommendations:
    post:
      summary: 获取路由推荐
      description: 根据该账户自行上报的 Yuno 之外的支付历史，对候选服务提供商排序，并返回推荐的候选项和完整排名。按组织启用。
      operationId: get-routing-recommendations
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - account_id
                - payment
                - candidates
              properties:
                account_id:
                  type: string
                  format: uuid
                  example: 7825fc5e-e50a-4248-9ae8-5d3786afb0be
                merchant_order_id:
                  type: string
                  maxLength: 255
                  example: order_45678
                payment:
                  type: object
                  required:
                    - amount
                    - country
                    - payment_method
                  properties:
                    amount:
                      type: object
                      required:
                        - currency
                        - value
                      properties:
                        currency:
                          type: string
                          example: USD
                          description: ISO 4217 货币代码。
                        value:
                          type: number
                          minimum: 0
                          example: 99
                    country:
                      type: string
                      example: US
                      description: ISO 3166-1 alpha-2 国家/地区代码。
                    payment_method:
                      type: object
                      required:
                        - type
                      properties:
                        type:
                          type: string
                          example: CARD
                        details:
                          type: object
                          description: 可选。用于细化统计分段的支付方式详情。
                          properties:
                            card:
                              type: object
                              properties:
                                bin:
                                  type: string
                                  description: >-
                                    卡的 BIN，恰好为 6 位或 8 位数字。当该 BIN
                                    在支付所在国家有足够的上报历史时，用于选择 BIN 统计分段（优先 8 位
                                    BIN，其次前 6
                                    位）；否则排名回退到国家和支付方式分段。其他任何值都会被忽略，绝不会被拒绝。
                                  example: '45717360'
                    initiation:
                      type: string
                      enum:
                        - CIT
                        - MIT
                      example: CIT
                candidates:
                  type: array
                  minItems: 2
                  maxItems: 20
                  items:
                    type: object
                    required:
                      - provider_id
                    properties:
                      provider_id:
                        type: string
                        example: ADYEN
                      merchant_connection_id:
                        type: string
                        example: acct-adyen-us-1
                optimize_for:
                  type: string
                  enum:
                    - APPROVAL_RATE
                    - LATENCY
                    - BALANCED
                  default: APPROVAL_RATE
                  example: APPROVAL_RATE
            example:
              account_id: 7825fc5e-e50a-4248-9ae8-5d3786afb0be
              merchant_order_id: order_45678
              payment:
                amount:
                  currency: USD
                  value: 99
                country: US
                payment_method:
                  type: CARD
              candidates:
                - provider_id: ADYEN
                  merchant_connection_id: acct-adyen-us-1
                - provider_id: STRIPE
                - provider_id: DLOCAL
              optimize_for: APPROVAL_RATE
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  recommendation_id:
                    type: string
                    format: uuid
                    example: 3e37f0c9-e9cd-4d85-9e1a-ec5f09af296d
                  merchant_order_id:
                    type: string
                    nullable: true
                    example: order_45678
                  optimized_for:
                    type: string
                    enum:
                      - APPROVAL_RATE
                      - LATENCY
                      - BALANCED
                    example: APPROVAL_RATE
                  decision_source:
                    type: string
                    enum:
                      - MERCHANT_HISTORY
                      - INSUFFICIENT_DATA
                    example: MERCHANT_HISTORY
                  recommended:
                    type: object
                    properties:
                      provider_id:
                        type: string
                        example: ADYEN
                      merchant_connection_id:
                        type: string
                        nullable: true
                        example: acct-adyen-us-1
                  ranking:
                    type: array
                    items:
                      type: object
                      properties:
                        provider_id:
                          type: string
                          example: ADYEN
                        merchant_connection_id:
                          type: string
                          nullable: true
                          example: acct-adyen-us-1
                        approval_rate:
                          type: number
                          nullable: true
                          example: 0.9987
                        avg_latency_ms:
                          type: integer
                          nullable: true
                          example: 180
                        sample_size:
                          type: integer
                          example: 767
        '400':
          description: Bad Request
          content:
            application/json:
              schema:
                type: object
                properties:
                  code:
                    type: string
                    example: INVALID_PARAMETERS
                  messages:
                    type: array
                    items:
                      type: string
                    example:
                      - The field 'candidates' has a value less than allowed.
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                type: object
                properties:
                  code:
                    type: string
                    example: INVALID_CREDENTIALS
                  messages:
                    type: array
                    items:
                      type: string
                    example:
                      - Invalid Credentials
        '403':
          description: Forbidden
          content:
            application/json:
              schema:
                type: object
                properties:
                  code:
                    type: string
                    example: PRODUCT_NOT_ENABLED
                  messages:
                    type: array
                    items:
                      type: string
                    example:
                      - >-
                        The Routing Recommendations API is enabled per
                        organization. Contact your Key Account Manager (KAM) to
                        activate it.
        '502':
          description: 网关错误
          content:
            application/json:
              schema:
                type: object
                properties:
                  code:
                    type: string
                    example: ROUTING_UPSTREAM_ERROR
                  messages:
                    type: array
                    items:
                      type: string
                    example:
                      - routing service unavailable.
components:
  securitySchemes:
    sec0:
      type: apiKey
      in: header
      name: PUBLIC-API-KEY
      x-default: <Your PUBLIC-API-KEY>
    sec1:
      type: apiKey
      in: header
      name: PRIVATE-SECRET-KEY
      x-default: <Your PRIVATE-SECRET-KEY>

````

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