API การทํางาน
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
Actions API มีปลายทางเพื่อช่วยสร้าง จัดการ และทดสอบการดำเนินการ
ไลบรารีของไคลเอ็นต์ (Node.js)
แม้ว่าคุณจะใช้บริการ RESTful ของ Actions API ได้โดยการส่งคำขอ HTTP ไปยังเซิร์ฟเวอร์โดยตรง แต่เรามีไลบรารีไคลเอ็นต์ที่จะช่วยให้คุณเข้าถึงปลายทางจาก Node.js ได้ง่ายขึ้น ไลบรารีไคลเอ็นต์ Actions API ช่วยให้คุณจัดการและทดสอบการดําเนินการโดยใช้ปลายทางต่างๆ ได้
เช่น โค้ดด้านล่างจะเรียกใช้ปลายทาง 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 ในเอกสารประกอบไลบรารีและข้อมูลอ้างอิง REST API ของ REST
ข้อกำหนด
ข้อกำหนดต่อไปนี้มีผลกับคำขอที่ส่งไปยัง Actions API
ขนาดเพย์โหลดคำขอ
คำขอที่ส่งไปยัง Actions API ต้องมีขนาดไม่เกิน 10 MB ซึ่งรวมถึง
อุปกรณ์ปลายทางสตรีมมิงของไคลเอ็นต์ ซึ่งคำขอแต่ละรายการในสตรีมต้องมีขนาดไม่เกิน 10 MB
หรือน้อยกว่านั้น
หากเพย์โหลดมีขนาดเกิน 10 MB คุณควรได้รับข้อผิดพลาด 400
จากการดำเนินการ
เซิร์ฟเวอร์ SDK
แนวทางปฏิบัติแนะนำ
ขอแนะนำให้ทำตามแนวทางปฏิบัติแนะนำต่อไปนี้
ที่คุณใช้ Actions API
เมื่อสร้างเครื่องมือหรือแอปพลิเคชันสำหรับผู้ใช้ คุณอาจต้องการ
ที่จะเรียกเก็บเงินและใช้สำหรับขีดจำกัดโควต้า แทนที่จะเป็นโปรเจ็กต์ไคลเอ็นต์ของคุณ
หากต้องการระบุโปรเจ็กต์เพื่อจุดประสงค์ในการเรียกเก็บเงินและโควต้า ให้ตั้งค่า
ส่วนหัวของคำขอ x-goog-user-project
ค่าที่ถูกต้อง |
รหัสโปรเจ็กต์สําหรับพร็อพเพอร์ตี้ที่มีอยู่
โปรเจ็กต์ Google Cloud |
ตัวอย่าง |
x-goog-user-project: my-project |
รายละเอียด |
โปรเจ็กต์ที่ระบุในส่วนหัวใช้สำหรับขีดจำกัดโควต้าและเรียกเก็บเงิน
สำหรับค่าบริการที่เกี่ยวข้องกับคำขอดังกล่าว
|
ตั้งค่า User Agent ที่เหมาะสมโดยใช้ส่วนหัวของคำขอ user-agent
ช่วงเวลานี้
ช่วย API ในการพิจารณาว่าคำขอมาจากพาร์ทเนอร์หรือไม่
ข้อจำกัดที่ทราบ
ส่วนนี้จะอธิบายข้อจำกัดที่ทราบของ Actions API
ระยะหมดเวลาสำหรับปลายทางสตรีมมิงของไคลเอ็นต์
ข้อจำกัดนี้มีผลกับคำขอ HTTP ที่คุณทำกับไคลเอ็นต์สตรีมมิงเท่านั้น
ปลายทาง คำขอที่สร้างโดยใช้ไลบรารีของไคลเอ็นต์จะไม่ดำเนินการ
จากข้อจำกัดนี้
หากคุณใช้คำขอ HTTP เพื่อเรียก WritePreview
, CreateVersion
หรือ WriteDraft
เราจะ
ขอแนะนำให้คุณใช้การหมดเวลาเพื่อจัดการกับคำขอที่ไม่ได้
สำเร็จ
หากคุณได้รับส่วนหัวการตอบกลับที่ระบุรหัสสถานะอื่นที่ไม่ใช่ 200
โค้ดของคุณควรทำให้สตรีมสิ้นสุดลงหลังจากระยะเวลาหนึ่ง ฉบับนี้
มีผลกับปลายทางการสตรีมของไคลเอ็นต์เท่านั้น ตัวอย่างเช่น เครื่องมือ gactions
ซึ่งใช้ Actions API มีระยะหมดเวลา 5 วินาที
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-26 UTC
[[["เข้าใจง่าย","easyToUnderstand","thumb-up"],["แก้ปัญหาของฉันได้","solvedMyProblem","thumb-up"],["อื่นๆ","otherUp","thumb-up"]],[["ไม่มีข้อมูลที่ฉันต้องการ","missingTheInformationINeed","thumb-down"],["ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป","tooComplicatedTooManySteps","thumb-down"],["ล้าสมัย","outOfDate","thumb-down"],["ปัญหาเกี่ยวกับการแปล","translationIssue","thumb-down"],["ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด","samplesCodeIssue","thumb-down"],["อื่นๆ","otherDown","thumb-down"]],["อัปเดตล่าสุด 2025-07-26 UTC"],[[["\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."]]