Actions API
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Actions API 提供有助于构建、管理和测试 Action 的端点。
客户端库 (Node.js)
虽然您可以通过向服务器发出直接 HTTP 请求来使用 Actions API RESTful 服务,但我们提供了一个客户端库,可让您更轻松地从 Node.js 访问端点。借助 Actions API 客户端库,您可以使用各种端点管理和测试 Action。
例如,以下代码会调用 writePreview
端点,以根据提供的模型更新用户的项目预览:
import {ActionsSdkClient} from '@assistant/actions';
import { promisify } from 'util';
import * as stream from 'stream';
const PROJECT_ID = '<PROJECT_ID>';
const VERSION = 123;
const projectPath = `projects/${PROJECT_ID}`;
const versionPath = `projects/${PROJECT_ID}/versions/${VERSION}`;
async function publishPreview(projectPath, versionPath) {
const request = {
parent: projectPath,
previewSettings: {sandbox: {value: true}},
submittedVersion: {version: versionPath}
};
const client = new ActionsSdkClient();
const stream = client.writePreview(()=>{});
stream.write(request);
stream.end();
const finished = promisify(stream.finished);
await finished(stream);
}
如需了解 Actions API Node.js 客户端库的安装说明和参考资料,请参阅库和 Actions API REST 参考文档文档。
要求
以下要求适用于向 Actions API 发出的请求。
请求载荷大小
向 Actions API 发出的请求不得超过 10 MB。这包括
客户端流式传输端点,数据流中每个请求的大小必须为 10 MB
或更小。
如果您的载荷超过 10 MB,您应该会收到来自 Action 的 400
错误
SDK 服务器。
最佳做法
在以下情况下,强烈建议您遵循
使用 Actions API。
在为用户开发工具或应用时,您可能希望用户的
用于结算和用于配额限制的项目,而不是您的客户端项目。
如需指定用于结算和配额的项目,请设置
x-goog-user-project
请求标头。
有效值 |
现有项目的项目 ID
Google Cloud 项目 |
示例 |
x-goog-user-project: my-project |
详细信息 |
标头中指定的项目会用于计算配额限制并结算费用
与请求相关的费用
|
请使用 user-agent
请求标头设置适当的用户代理。这个
有助于 API 确定请求是否来自合作伙伴。
已知限制
本部分介绍了 Actions API 的已知限制。
客户端流式传输端点的超时
此限制仅适用于您向客户端流式传输发出的 HTTP 请求
端点。使用客户端库发出的请求不会
将受此限制的影响
如果您使用 HTTP 请求来调用 WritePreview
、CreateVersion
或 WriteDraft
,
我们建议您实施超时,以处理
成功。
如果您收到的响应标头指定 200
以外的状态代码,
您的代码应在一段时间后终止流式传输。此问题
只会影响客户端流式传输端点。例如,gactions
工具
使用 Actions API 的 SDK,超时时间为 5 秒。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003eThe Actions API offers endpoints for building, managing, and testing Actions.\u003c/p\u003e\n"],["\u003cp\u003eA Node.js client library simplifies interactions with the API, offering easier access to endpoints for managing and testing your Action.\u003c/p\u003e\n"],["\u003cp\u003eRequests to the Actions API must be 10 MB or less, including those made to client streaming endpoints.\u003c/p\u003e\n"],["\u003cp\u003eSetting the \u003ccode\u003ex-goog-user-project\u003c/code\u003e request header enables billing and quota management for user projects.\u003c/p\u003e\n"],["\u003cp\u003eWhen using HTTP requests with client streaming endpoints like \u003ccode\u003eWritePreview\u003c/code\u003e, it's recommended to implement a timeout to handle potential unsuccessful requests.\u003c/p\u003e\n"]]],["The Actions API facilitates Action development, management, and testing. A Node.js client library simplifies interaction with API endpoints. For instance, `writePreview` updates a project preview. Requests must be under 10MB, with the `x-goog-user-project` header recommended for billing and quota purposes. The `user-agent` header is advised for partner identification. Direct HTTP calls to streaming endpoints like `WritePreview` have timeout considerations, though the client library mitigates this.\n"],null,["# Actions API\n\nThe Actions API provides endpoints to help build, manage, and test your Action.\n\nClient Library (Node.js)\n------------------------\n\nWhile you can use the Actions API RESTful service by making direct HTTP requests to the server, we provide a client library that makes it easier to access the endpoints from Node.js. The [Actions API client library](https://github.com/actions-on-google/assistant-actions-nodejs) allows you to manage and test your Action using a variety of endpoints.\n\nFor example, the code below makes a call to the [`writePreview`](/assistant/actions/api/reference/rest/v2/projects.preview/write) endpoint to update a user's project preview based on the provided model: \n\n import {ActionsSdkClient} from '@assistant/actions';\n import { promisify } from 'util';\n import * as stream from 'stream';\n\n const PROJECT_ID = '\u003cPROJECT_ID\u003e';\n const VERSION = 123;\n\n const projectPath = `projects/${PROJECT_ID}`;\n const versionPath = `projects/${PROJECT_ID}/versions/${VERSION}`;\n\n async function publishPreview(projectPath, versionPath) {\n const request = {\n parent: projectPath,\n previewSettings: {sandbox: {value: true}},\n submittedVersion: {version: versionPath}\n };\n\n const client = new ActionsSdkClient();\n const stream = client.writePreview(()=\u003e{});\n stream.write(request);\n stream.end();\n const finished = promisify(stream.finished);\n await finished(stream);\n }\n\nFor installation instructions and reference material for the Actions API Node.js client library, see the [library](/assistant/actions/api/(https:/github.com/actions-on-google/assistant-actions-nodejs)) and [Actions API REST reference](/assistant/actions/api/reference/rest) documentation.\n\nRequirements\n------------\n\nThe following requirements apply to requests made to the Actions API.\n\n### Request payload size\n\nRequests made to the Actions API must be 10 MB or less. This includes\nclient streaming endpoints, where each request in the stream must be 10 MB\nor less.\n\nIf your payload exceeds 10 MB, you should receive a `400` error from the Actions\nSDK server.\n\nBest practices\n--------------\n\nThe following best practices are strongly recommended when\nyou use the Actions API.\n\n### Set the x-goog-user-project request header\n\nWhen building a tool or application for your users, you may want a user's\nproject to be billed and used for quota limits, rather than your client project.\nTo specify a project for billing and quota purposes, set the\n`x-goog-user-project` request header.\n\n|--------------|------------------------------------------------------------------------------------------------------------------|\n| Valid Values | The [Project ID](/docs/overview#projects) for an existing Google Cloud project |\n| Example | `x-goog-user-project: my-project` |\n| Details | The project specified in the header is used for quota limits and billed for charges associated with the request. |\n\n### Set the user-agent request header\n\nSet an appropriate user agent using the `user-agent` request header. This\nhelps the API determine if requests originate from partners.\n\nKnown limitations\n-----------------\n\nThis section describes known limitations of the Actions API.\n\n### Timeout for client streaming endpoints\n\nThis limitation only applies to HTTP requests you make to client streaming\nendpoints. Requests made using the [client library](#client-library) are not\naffected by this limitation.\n\nIf you are using HTTP requests to call `WritePreview`, `CreateVersion`, or `WriteDraft`, we\nrecommend that you implement a timeout to handle requests that are not\nsuccessful.\n\nIf you receive a response header that specifies a status code other than `200`,\nyour code should terminate the stream after a certain period of time. This issue\nonly affects client streaming endpoints. For example, the `gactions` tool,\nwhich uses the Actions API, has a 5-second timeout."]]