You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Azure API管理器中添加剩余配额至响应头?

Azure API管理器:在响应头返回剩余订阅配额次数

可行,具体实现方法是通过API管理策略的上下文对象context获取配额剩余次数,修改outbound阶段的set-header策略即可。

修改后的完整策略

<policies>
    <inbound>
        <base />
        <quota-by-key calls="3" renewal-period="0" counter-key="@(context.Subscription?.Key ?? "anonymous")" increment-condition="@(context.Response.StatusCode >= 200 && context.Response.StatusCode < 300)"/>
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
        <set-header name="quota-left" exists-action="override">
            <value>@(context.Request.Quota.Remaining.ToString())</value>
        </set-header>
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

关键说明

  • quota-by-key在inbound阶段执行后,context.Request.Quota会被更新为当前订阅的配额状态,其中Remaining属性就是剩余可调用次数
  • 将exists-action设为override,确保每次响应只返回最新的剩余配额值,不会重复添加响应头
  • 若需要同时返回总配额或重置时间,可扩展响应头,比如添加quota-limit: @(context.Request.Quota.Limit.ToString())、quota-reset: @(context.Request.Quota.Reset.ToString())(仅当renewal-period>0时Reset值有效)

内容的提问来源于stack exchange,提问作者Menno Fesevur

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 20:55:16