DeepSeek 网摘deepseek.wang
EN
📄 查看英文原文

API · 2026-04-25 · 机器翻译,仅供参考

构建DeepSeek API Webhooks:实用指南

API·April 25, 2026·By DS Guide Editorial
DeepSeek API webhooks tutorial card showing the five practical patterns since DeepSeek has no native webhook surface as of April 2026: build a thin relay in your own server, combine the streaming endpoint with your own queue for async jobs, push results from the relay to your callback URL, and route inbound webhooks from Stripe, GitHub, or Slack into DeepSeek directly.

“如何让DeepSeek在生成完成后调用我的服务器?”这个问题在开发者超越一次性脚本时就会出现。诚实的答案是大多数文章都会跳过的:截至2026年4月25日,DeepSeek API的webhooks并不是DeepSeek平台官方文档化的第一方功能。官方接口是一个同步的端点,可选的Server-Sent Events流——没有“注册回调URL,我们将向您POST数据”的服务。这并不意味着您不能构建webhook风格的操作;这意味着您需要自己使用一个薄的中继来构建。本指南展示了在生产环境中实际可行的模式,包括代码、成本和权衡。 POST /chat/completions 当发生某些事情时——比如支付成功、任务完成、消息到达——它会将信息反馈给您。Stripe、GitHub和Twilio都提供了这种模式。DeepSeek没有。DeepSeek API使用与OpenAI/Anthropic兼容的格式——通过修改配置,您可以使用OpenAI或Anthropic SDK或兼容软件来访问DeepSeek API。这两个上游API都是请求/响应的,可选流式传输,DeepSeek也反映了这一层面。没有

“DeepSeek API webhooks”究竟是什么意思

Diagram of DeepSeek API webhook flow from DeepSeek emitting an event to a developer endpoint with retry

在通常意义上,webhook是一个你注册到服务的URL,以便它可以 POST 所以当人们搜索“deepseek api webhooks”时,他们通常在问以下三个问题之一: /webhooks 端点,无需回调注册,也没有你可以订阅的事件总线。

——“我能发射并忘记一个长时间的生成吗?”是的,通过将流与您自己的队列结合。

  • 推送交付 — “DeepSeek能否将结果POST到我的服务器?” 不能直接。你需要构建一个中继。
  • 异步任务 — “Can I fire-and-forget a long generation?” Yes, by combining streaming with your own queue.
  • 入站触发器 — “Stripe/GitHub/Slack的webhook能否调用DeepSeek?” 可以,这是最简单的情况。

本文涵盖了所有三种情况。如果你想先了解底层表面,那么 DeepSeek API documentation 是一个好的先决条件,并且 DeepSeek OpenAI SDK compatibility 涵盖了我们将要使用的线格式。

简述DeepSeek API接口

在我们连接任何东西之前,基准。DeepSeek 的当前一代是 DeepSeek V4于2026年4月24日发布,以两个MIT许可下的开放权重专家混合模型形式发布: deepseek-v4-pro (1.6T total / 49B active parameters, frontier tier) and deepseek-v4-flash (284B / 13B 激活,成本效益级别)。两者默认使用1,000,000个token的上下文窗口,最多输出384,000个token,并且两者都通过请求参数而不是单独的模型ID来暴露思考模式。

Chat requests hit POST /chat/completions, the OpenAI-compatible endpoint at https://api.deepseek.com。API支持在https://api.deepseek.com上的OpenAI兼容的聊天完成格式,以及在https://api.deepseek.com/anthropic上的Anthropic兼容格式。如果您正在维护旧版本的集成,模型名称deepseek-chat和deepseek-reasoner将于2026年07月24日被弃用——为了兼容性,它们分别对应于deepseek-v4-flash的非思考模式和思考模式。确切的退役日期是 2026-07-24 at 15:59 UTC; migrating is a one-line model= 交换,没有 base_url 变化。

对于 webhook 风格的工作,一个属性比其他任何属性都重要:API 是 stateless。

最小请求

这里是一个使用OpenAI SDK模式的Python最小调用示例:

from openai import OpenAI

client = OpenAI(
    base_url="https://api.deepseek.com",
    api_key="YOUR_KEY",
)

resp = client.chat.completions.create(
    model="deepseek-v4-flash",
    messages=[{"role": "user", "content": "Summarise this ticket: ..."}],
)
print(resp.choices[0].message.content)

如果你之前从未调用过, DeepSeek API getting started 全程演示了第一个请求的调用过程,并且 how to get a DeepSeek API key 涵盖了控制台操作。

模式1:触发DeepSeek的入站webhooks

这是人们常犯的错误。您已经从另一个服务(如Stripe、GitHub、Slack、Linear、Intercom)获得了webhook,并且希望DeepSeek对有效负载执行某些操作。DeepSeek不是webhook;您的处理程序才是。

结构如下:

  1. 第三方服务POST到 https://yourapp.com/hooks/stripe.
  2. 你的处理程序验证签名。
  3. 你同步调用DeepSeek并根据结果操作(写入数据库、发布到Slack等)。
  4. 你返回 200 OK 到第三方服务。

一个Python中的FastAPI草图:

from fastapi import FastAPI, Request
from openai import OpenAI

app = FastAPI()
client = OpenAI(base_url="https://api.deepseek.com", api_key="...")

@app.post("/hooks/support")
async def support_hook(req: Request):
    payload = await req.json()
    # verify signature here
    summary = client.chat.completions.create(
        model="deepseek-v4-flash",
        messages=[
            {"role": "system", "content": "Summarise support tickets in 2 lines."},
            {"role": "user", "content": payload["ticket"]["body"]},
        ],
        max_tokens=200,
    )
    # write summary to your DB / Slack / Linear
    return {"ok": True}

两个警告。首先,大多数webhook发送者期望在几秒内收到响应。如果您的DeepSeek调用可能需要30秒或更长时间(长思考模式、大量输出), 不要 阻塞webhook处理程序——返回 200 立即排队作业,并在工作器中处理它。其次,当达到并发限制时,API会立即返回HTTP 429状态码,并且每个账户暴露的限制会根据实时流量压力和短期历史使用情况动态调整。因此,一波入站的webhooks可能会产生一波429错误;排队重试而不是使原始webhook失败。

模式2:通过流式中继进行出站交付

当人们询问“DeepSeek API webhooks”时,通常就是这个意思。您希望DeepSeek能够 POST 将部分或最终结果发送到您控制的URL。由于DeepSeek本身不提供这种功能,您需要在您的客户端和API之间放置一个小型的中继器。

中继:

  1. 从你的应用程序(带有 callback_url).
  2. 使用DeepSeek进行调用 stream=true 并消耗SSE流。
  3. 将每个delta(或仅最终结果)POST到 callback_url.

流式传输原语来自DeepSeek本身。设置“stream”:true以接收仅数据的服务端发送事件(SSE),作为模型生成输出。您在服务器端消费它,然后将下游调用者期望的任何形状的数据转发出去。

最小中继工作器

这个Python工作器将队列消息视为“触发器”,并将 callback_url 作为webhook目标:

import httpx
from openai import OpenAI

client = OpenAI(base_url="https://api.deepseek.com", api_key="...")

def run_job(job):
    stream = client.chat.completions.create(
        model="deepseek-v4-pro",
        messages=job["messages"],
        reasoning_effort="high",
        extra_body={"thinking": {"type": "enabled"}},
        stream=True,
    )

    final_text = []
    for chunk in stream:
        delta = chunk.choices[0].delta
        if delta.content:
            final_text.append(delta.content)

    httpx.post(
        job["callback_url"],
        json={"job_id": job["id"], "content": "".join(final_text)},
        headers={"X-Signature": sign(job["id"])},
        timeout=10,
    )

When thinking mode is enabled, the response returns reasoning_content alongside the final content。如果您的下游消费者需要跟踪,捕获 delta.reasoning_content 在循环中,并与 delta.content 一起发送。

设计回调有效负载

适合人们消费的webhooks有一些共同特点。匹配它们:

  • 稳定的模式。 版本化的信封: {"version": "1", "event": "deepseek.completion", "data": {...}}.
  • 签名。 使用每个接收者的秘密,在原始体上使用HMAC-SHA256,在 X-Signature 头部。
  • 幂等。 包含一个稳定的 job_id 因此接收者可以在重试时去重。
  • 带有退避策略的重试。 如果接收者响应非2xx状态码,至少重试一小时并带有抖动。
  • 相关的。 转发DeepSeek x-request-id 响应头,以便支持票务可以追踪。

模式3:无需中继的长时间运行作业

如果您无法运行中继器(无服务器冷启动限制、没有队列基础设施),有一个更薄的选择:轮询。您的客户端提交作业,您的工人在DeepSeek完成时将其流式传输并写入键值存储,然后客户端轮询 /jobs/{id} 端点直到 状态=完成。这并不是webhooks——但它解决了相同的异步结果问题,并且是大多数没有中继器的团队最终得到的结果。

这里有一个操作注意事项。在调度压力下,非流式请求在等待时可能会返回空行,流式请求可能会返回保持连接的注释,如果在10分钟后推理尚未开始,服务器将关闭连接。一个简单的HTTP客户端会在冷队列上超时;您的工组需要处理重连,并将早期关闭视为重试,而不是作业失败。

参考:对异步工作重要的参数

Parameter 目的 关于webhook风格使用的说明
model Model tier deepseek-v4-flash 用于聊天/分类, deepseek-v4-pro 用于代理或编码工作。
stream 启用SSE Set true 在中继器中,以便可以转发部分增量。
reasoning_effort Thinking mode "high" with extra_body={"thinking": {"type": "enabled"}}; "max" 以实现最大努力。
temperature 随机性 代码/数学为0.0,数据分析为1.0,通用聊天为1.3,创意写作为1.5。
max_tokens 输出限制 在V4上最高可达384,000。使用JSON模式时,请慷慨设置以防止截断。
response_format JSON mode 设计为返回有效的JSON,但不保证;请使用“json”一词和示例模式进行提示,并处理偶尔的空内容。
tools 函数调用 标准OpenAI形状;在思考和非思考模式下均有效。

关于流的更全面处理,请参阅 DeepSeek API streaming;对于工具调用有效负载形状, DeepSeek API function calling.

速率限制、重试和429处理

出站webhook交付本质上是突发性的——一个Stripe事件可以扩展成十个DeepSeek调用。DeepSeek目前将API速率限制描述为基于服务器负载的动态并发限制。对中继设计的含义:

  • 在工作器中限制并发不在上游调用者中。将工组限制在您已测量的数量,而不是您猜测的数量。
  • 带抖动的指数退避 在429和5xx错误上——起始时间为1秒,最大值为60秒,尝试10次后放弃。
  • 尊重 Retry-After 如果存在。
  • 在您的死信队列中暴露DeepSeek请求ID 以便支持请求可以引用它。

The DeepSeek API rate limits 页面涵盖了详细信息,并且 DeepSeek API error codes 映射了您将看到的大多数状态码。

成本示例:大规模的webhooks扇出

假设您每月运行一百万个事件的支持入站webhooks。每个事件触发一个 deepseek-v4-flash 使用2,000个token的系统提示(跨调用缓存),200个token的用户消息(每个调用不缓存),以及300个token的响应。截至2026年4月,V4-Flash列出的价格是每1M token $0.0028缓存命中 / $0.14缓存未命中 / $0.28输出(从2026年4月26日起缓存命中价格从$0.0028削减——与未命中相比减少了约98%)——请在[5]上验证——大约是Flash账单的3倍(列表$0.0145 / $1.74 / $3.48 → $1,421.00,约14倍于Flash)。对于呈三叉形分布的webhook分发,V4-Flash几乎总是正确的层级;将V4-Pro保留用于基准提升足以证明账单的案例。不要跳过未缓存输入行:每个新的用户消息都是对缓存前缀的未命中,直到模型之前见过它。默认为所有用户启用DeepSeek磁盘上下文缓存——如果后续请求共享重叠的前缀,重复的前缀可以计为缓存命中,从而降低有效输入成本;可以命中缓存的是重复的前缀,例如相同的系统提示或重复的few-shot示例,您可以检查 DeepSeek API pricing page before committing.

Cached input    : 2,000 × 1,000,000 = 2,000,000,000 tokens × $0.0028/M = $5.60
Uncached input  :   200 × 1,000,000 =   200,000,000 tokens × $0.14/M   = $28.00
Output          :   300 × 1,000,000 =   300,000,000 tokens × $0.28/M   = $84.00
                                                                        -------
Total                                                                    $117.60

The same workload on deepseek-v4-pro at the 75% promo through 2026-05-31 ($0.003625 / $0.435 / $0.87 每100万个令牌) lands at $355.25 — about 3× the Flash bill (list $0.0145 / $1.74 / $3.48 → $1,421.00, ~14× Flash). For triage-shaped webhook fan-out, V4-Flash is almost always the right tier; reserve V4-Pro for the cases where the benchmark lift justifies the bill. Do not skip the uncached-input line: each new user message is a miss against the cached prefix until the model has seen it before. DeepSeek Context Caching on Disk is enabled by default for all users — if later requests share an overlapping prefix the repeated prefix can count as a cache hit, which reduces effective input cost; what can hit the cache is repeated prefixes such as the same system prompt or repeated few-shot examples, and you can inspect usage.prompt_cache_hit_tokens and usage.prompt_cache_miss_tokens 在响应中。

安全清单

Server log stream on a monitor showing DeepSeek API webhook POST requests with statuses and event types
  • 验证传入签名 在调用DeepSeek之前对每个第三方webhook进行验证。不要在伪造的事件上消耗代币。
  • 使用HMAC-SHA256对原始正文和文档进行签名,并记录验证配方。 轮换API密钥
  • 移除个人身份信息(PII) 在任何触摸到公共日志或截图的键上。使用控制台分别的键进行预演和生产。
  • 永远不要在浏览器代码中暴露您的DeepSeek密钥。 从你可以使用的提示符——API将请求发送到DeepSeek运营的基础设施,你的隐私姿态应该相匹配。
  • 所有调用都通过您的中继进行,其中包含密钥。 如果您正在将DeepSeek集成到Node.js后端,

这适合在哪里

在JavaScript中覆盖相同的模式。对于以Python为主的堆栈, DeepSeek Node.js integration covers the same pattern in JavaScript. For Python-first stacks, DeepSeek Python integration 是配套指南。如果您仍在决定要针对哪个模型层级,[1] 中心收集了其余的参考信息。 DeepSeek API docs and guides hub gathers the rest of the reference.

DeepSeek AI Guide is an independent resource and is not affiliated with DeepSeek or its parent company. Model IDs, pricing and API behaviour change; check the official DeepSeek documentation and pricing page before committing to a production decision.

DeepSeek是否原生支持webhooks?

不。截至2026年4月25日,官方DeepSeek API公开了一个针对完整表面的同步接口。 POST /chat/completions 端点具有可选的服务器端事件流,以及在同一基础URL上的Anthropic兼容界面。没有文档化的webhook注册、回调URL或事件订阅系统。要获取推送交付,你需要构建一个小型中继,该中继消费流并将其POST到自己的URL。参见 DeepSeek API documentation 运行一个调用DeepSeek的worker,使用

当DeepSeek生成完成时,我如何接收事件?

指南涵盖了SSE消费者端。 stream=true,累积增量,并在流关闭时将最终结果POST到你的回调URL。这是本文正文中的中继模式。如果轮询是可以接受的,你还可以将结果写入键值存储,并让客户端轮询状态端点。The DeepSeek API streaming 在DeepSeek中使用负载作为输入。返回

Stripe或GitHub webhook能否触发DeepSeek?

是的——这是最简单的情况,也是大多数人实际需要的情况。在你的处理程序中接收第三方webhook,验证其签名,然后调用 POST /chat/completions 展示了基本调用。 200 快速发送给发送者;如果DeepSeek调用缓慢,将其排队并在工作器中处理,以免超时原始webhook。 DeepSeek API getting started 。有关当前行为,请参阅

DeepSeek webhook扇出的速率限制是多少?

DeepSeek不会发布每个密钥每秒请求的固定上限。平台根据服务器负载和每个账户的历史记录应用动态并发限制;一旦超过此限制,API将返回HTTP 429。对于webhook广播,绑定工作器并发,使用指数退避和抖动重试429,并遵守 Retry-After,有关重试模式,请参阅 DeepSeek API rate limits 。 DeepSeek API best practices 参考涵盖了匹配状态语义。

为什么我的长时间DeepSeek请求会挂起或提前关闭?

在调度压力下,DeepSeek在等待时可能会发出空行(非流式)或SSE保持活动状态注释(流式),如果十分钟内推理未开始,服务器将关闭连接。配置显式的连接和读取超时,将早期关闭视为可重试的,并确保您的反向代理或无服务器运行时不终止长时间运行的流式响应。 DeepSeek API error codes reference covers the matching status semantics.

来源s & Methodology

Last source check: April 30, 2026

Primary sources

Methodology

Pricing figures were checked against the 官方DeepSeek定价页面 and normalised into estimated monthly costs using sample token usage scenarios. Actual costs may vary depending on caching, input/output ratio, regional availability, and provider updates.

Data confidence

High — figures checked directly against the official pricing page on the review date.

Editorial note

DeepSeek occasionally runs promotional discounts that are not always reflected in the headline page. The pricing dataset on this site auto-refreshes monthly; this article reflects the dataset on the date shown above.