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

# 取消订阅

> 在当前计费周期结束时取消用户的活跃订阅。

## 概述

取消用户订阅。订阅在当前计费周期结束前仍然有效，到期后不再续费。需要 JWT 访问令牌。

<Note>
  \*\*第三方集成：\*\*请改用 `POST /subscriptions/cancel/api` 并以 API Key 鉴权。
</Note>

<Warning>
  本操作安排在账期结束时取消。用户在 `currentPeriodEnd` 之前仍保有访问权限。如需立即终止，请使用账单门户。
</Warning>

## 认证

<ParamField header="Authorization" type="string" required>
  JWT 访问令牌。格式：`Bearer YOUR_ACCESS_TOKEN`
</ParamField>

<RequestExample>
  ```bash cURL theme={null}
  curl -X POST https://api.llmgenerator.com/api/v1/subscriptions/cancel \
    -H "Authorization: Bearer YOUR_ACCESS_TOKEN"
  ```

  ```javascript JavaScript theme={null}
  const response = await fetch('https://api.llmgenerator.com/api/v1/subscriptions/cancel', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer YOUR_ACCESS_TOKEN'
    }
  });

  const data = await response.json();
  if (data.success) {
    console.log('Subscription will end on:', data.cancellationDate);
  }
  ```

  ```python Python theme={null}
  import requests

  response = requests.post(
      'https://api.llmgenerator.com/api/v1/subscriptions/cancel',
      headers={'Authorization': 'Bearer YOUR_ACCESS_TOKEN'}
  )
  data = response.json()
  if data['success']:
      print(f"Subscription will end on: {data['cancellationDate']}")
  ```
</RequestExample>

<ResponseExample>
  ```json Success theme={null}
  {
    "success": true,
    "message": "Subscription cancelled successfully",
    "cancellationDate": "2024-02-15T00:00:00Z"
  }
  ```

  ```json No Active Subscription theme={null}
  {
    "message": "No active subscription to cancel"
  }
  ```
</ResponseExample>

## 响应字段

<ResponseField name="success" type="boolean">
  取消请求是否成功受理。
</ResponseField>

<ResponseField name="message" type="string">
  可读状态说明。
</ResponseField>

<ResponseField name="cancellationDate" type="string">
  订阅将结束的 UTC 时间（ISO 8601，仅在成功时返回）。
</ResponseField>

## 错误响应

<ResponseField name="400">
  错误请求——无活跃订阅可取消，或无法通过 API 取消当前订阅。
</ResponseField>

<ResponseField name="401">
  未授权——令牌无效或缺失。
</ResponseField>

## 取消后的行为

| 场景             | 行为                |
| -------------- | ----------------- |
| 活跃订阅           | 标记为周期结束时取消        |
| 已取消            | 返回错误              |
| 无订阅            | 返回错误              |
| 无 Stripe 订阅 ID | 返回错误（无法通过 API 取消） |

## 重新订阅

用户若需恢复订阅，可以：

1. 使用账单门户（`/subscriptions/portal`）
2. 或在当前订阅结束后重新订阅

## 最佳实践

* **二次确认**：取消前应弹出确认
* **收集原因**：可询问取消原因以改进产品
* **挽留策略**：可考虑折扣或暂停选项
* **清晰提示**：明确展示权益截止日期

***

## POST /subscriptions/cancel/api

使用 API Key 取消订阅，适合第三方集成。

### 认证

需要 API Key：

```
Authorization: Bearer llmgen_your_api_key_here
```

### 响应

与 `/subscriptions/cancel` 相同。

<RequestExample>
  ```bash cURL theme={null}
  curl -X POST https://api.llmgenerator.com/api/v1/subscriptions/cancel/api \
    -H "Authorization: Bearer llmgen_your_api_key_here"
  ```

  ```javascript JavaScript theme={null}
  const response = await fetch('https://api.llmgenerator.com/api/v1/subscriptions/cancel/api', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer llmgen_your_api_key_here'
    }
  });

  const data = await response.json();
  if (data.success) {
    console.log('Subscription will end on:', data.cancellationDate);
  }
  ```

  ```python Python theme={null}
  import requests

  response = requests.post(
      'https://api.llmgenerator.com/api/v1/subscriptions/cancel/api',
      headers={'Authorization': 'Bearer llmgen_your_api_key_here'}
  )
  data = response.json()
  if data['success']:
      print(f"Subscription will end on: {data['cancellationDate']}")
  ```
</RequestExample>
