---
title: "Update User Plan"
method: POST
path: "/admin/users/{user_id}/plan"
---

# Update User Plan

`POST /admin/users/{user_id}/plan`

## Request

- Base URL: `https://api.twtapi.com` (the document declares no server; this is the origin it was published from)
- URL: `https://api.twtapi.com/admin/users/{user_id}/plan`
- Auth: none declared

把用户切换到指定 API 套餐（同时覆盖限流值、可选重置积分、刷新到期时间）。

参数：
    body.plan_id: 目标套餐，必须是 plan_type='api' 且 is_active 的套餐，
        否则 404，避免把用户挂到已下架/非 API 套餐上。
    body.reset_credits: 是否把积分重置为套餐额度（按 monthly_limit 换算）。
    body.validity_period: 有效期档位，未传则用套餐自带档位，再兜底 "1m"。

副作用：一次事务内改 User + 追加 CreditLog 后 commit；随后清用户鉴权缓存和
C 端看板缓存，最后（如需）把积分增量同步进 Redis。Redis 失败只记日志不抛错，
与 adjust_credits 的取舍一致：MySQL 为准，Redis 是加速层。

事务边界：所有字段改动都攒到同一次 commit，中间不落库；因此下面的
validity_period 校验失败直接 return 时，本次会话里的改动不会被提交（被丢弃）。

## Path parameters

- `user_id` integer, required

## Request body

- AdminUserPlanUpdate: 管理员为用户换套餐。 reset_credits=True 时会按新套餐重置剩余额度（默认行为，符合「买套餐」直觉）； 请按需关闭以免抹掉用户已充值购买的余额。 validity_period 为空表示沿用套餐自身的有效期。
  - `plan_id` integer, required
  - `reset_credits` boolean
  - `validity_period` string, nullable

## Response `200`

Successful Response

- unknown

## Other responses

- `422`: Validation Error

---

[API](https://skmtc.dev/twtapi/apis/twtapi-backend.md) · [All operations](https://skmtc.dev/twtapi/apis/twtapi-backend/llms.txt) · [OpenAPI document](https://skmtc.dev/twtapi/apis/twtapi-backend/revisions/5295976febaf?raw)
