Firestore
Manage Google Cloud Firestore databases via REST API
This skill is built on top of the official Firebase Firestore REST API reference documentation: https://firebase.google.com/docs/firestore/reference/rest
It enables you to interact with Google Cloud Firestore using the Firestore REST API through curl commands. It uses gcloud auth print-access-token to obtain authentication tokens, allowing you to perform Create, Read, Update, and Delete (CRUD) operations on Firestore documents and collections.
For related documentation:
Requirements
This skill requires curl and gcloud CLI.
For full installation and setup instructions, see installation.md.
Credentials & Environment
This skill uses OAuth 2.0 access tokens generated by gcloud auth print-access-token. The token is valid for a limited time (typically 1 hour) and inherits the permissions of the authenticated Google Cloud account.
This skill must run only with a dedicated service account context. Do not use personal user credentials or broad admin identities.
Before any operation, generate a fresh access token:
CODEBLOCK0
Before any operation, verify the active identity is a service account:
CODEBLOCK1
If the active account is not a service account (for example, it does not end with gserviceaccount.com), stop and ask the user to switch credentials before proceeding.
Security Recommendations:
- - Use a dedicated, least-privilege service account for automation tasks. Never use your personal or admin account.
- Test in a sandbox or development project before running commands against production.
- Verify your active project with
gcloud config list before executing commands. - Tokens expire after approximately 1 hour — regenerate if you encounter 401 Unauthorized errors.
- The token inherits ALL permissions of the authenticated account, including read access to sensitive data.
- Revoke tokens immediately if you suspect unauthorized access: INLINECODE7
- Audit activity regularly by reviewing Cloud Audit Logs for the project.
Security Considerations
Important: This skill can access Firestore data with the same permissions as the authenticated Google Cloud account. For safety, this skill requires explicit user approval before executing any operation, including read-only operations.
To minimize risk:
- 1. Only use this skill with service accounts that have the minimum required Firestore permissions
- Use separate projects for development/testing and production environments
- Review the
gcloud config list output before allowing any operations - Grant only
roles/datastore.viewer for read-only access or roles/datastore.user for limited read/write - Never use
roles/datastore.owner or roles/owner with this skill - Monitor Cloud Audit Logs for unexpected Firestore API calls
What You Can Do
You can perform the following operations on Firestore databases:
- - Create — Insert new documents into collections
- Read — Query documents with filters and conditions
- Update — Modify specific fields in existing documents using updateMask
- Delete — Remove documents from collections
- List — Retrieve all documents in a collection
- Batch operations — Perform multiple writes in a single atomic transaction
All operations use the Firestore REST API endpoint:
CODEBLOCK2
Workflow
Before executing any Firestore operation, you MUST follow this workflow:
- 1. Check active context — Run
gcloud config list --format='text(core.account,core.project)' to display the active account and project. Present this to the user so they are aware of which credentials and project will be used.
- 2. Generate access token — Always start by obtaining a fresh access token:
CODEBLOCK3
- 3. Construct the curl command — Build the appropriate curl command based on the operation:
- Use the correct HTTP method (POST for create/query, GET for read, PATCH for update, DELETE for delete)
- Include the
Authorization: Bearer $ACCESS_TOKEN header
- Set
Content-Type: application/json for requests with body
- Use the correct API endpoint for the project and collection
- 4. For all operations (read and write) — Present the full curl command to the user and wait for explicit approval before executing. See the Approval Policy section below.
- 5. Execute the command and parse the JSON response.
Important Rules
- - Always generate a fresh token first — Run
ACCESS_TOKEN=$(gcloud auth print-access-token) before any operation. - Use proper JSON formatting — Firestore requires specific field value types (stringValue, booleanValue, integerValue, etc.).
- Document ID generation — When creating documents, if you don't specify
?documentId=YOUR_ID in the URL, Firestore will automatically generate a unique document ID. - Include field paths in updateMask — When updating, use
updateMask.fieldPaths to specify which fields to update. - Never execute any command autonomously — always present the full curl command to the user and wait for explicit approval before running it, including read-only operations.
- Parse responses carefully — Firestore returns data in a nested format with typed values.
- Verify project ID — Always confirm you're targeting the correct project before executing commands.
Approval Policy
All operations require explicit user confirmation before execution.
This includes:
- - Create — Creating new documents in collections
- Read / Query / Get / List — Retrieving documents or query results
- Update / Patch — Modifying existing document fields
- Delete — Removing documents permanently
- Batch writes — Any batch operation that modifies data
For every operation, the agent must:
- 1. Show the full curl command that will be executed.
- Display the active account and project context.
- Wait for the user to explicitly approve before running the command.
Firestore Data Types
Firestore uses typed field values in JSON. Common types:
- -
stringValue — Text strings - INLINECODE20 — Integer numbers (as strings)
- INLINECODE21 — Floating-point numbers
- INLINECODE22 — true/false
- INLINECODE23 — ISO 8601 timestamps
- INLINECODE24 — Arrays of values
- INLINECODE25 — Nested objects
Example document structure:
CODEBLOCK4
Few-Shot Prompting Examples
Few-shot prompts and full command examples are available in examples.md.
Common Query Operators
When constructing queries, use these operators in the fieldFilter.op field:
- -
EQUAL — Field equals value - INLINECODE28 — Field does not equal value
- INLINECODE29 — Field is less than value
- INLINECODE30 — Field is less than or equal to value
- INLINECODE31 — Field is greater than value
- INLINECODE32 — Field is greater than or equal to value
- INLINECODE33 — Array field contains value
- INLINECODE34 — Field value is in the provided array
- INLINECODE35 — Array field contains any of the provided values
Troubleshooting
For dedicated troubleshooting guidance, see troubleshooting.md.
Firestore
通过REST API管理Google Cloud Firestore数据库
此技能基于官方Firebase Firestore REST API参考文档构建:https://firebase.google.com/docs/firestore/reference/rest
它使您能够通过curl命令使用Firestore REST API与Google Cloud Firestore进行交互。它使用gcloud auth print-access-token获取认证令牌,允许您对Firestore文档和集合执行创建、读取、更新和删除(CRUD)操作。
相关文档:
要求
此技能需要curl和gcloud命令行工具。
完整的安装和设置说明,请参阅installation.md。
凭据与环境
此技能使用由gcloud auth print-access-token生成的OAuth 2.0访问令牌。令牌有效期有限(通常为1小时),并继承已认证Google Cloud账户的权限。
此技能必须仅在专用服务账户上下文中运行。请勿使用个人用户凭据或广泛的管理员身份。
在任何操作之前,生成新的访问令牌:
bash
ACCESS_TOKEN=$(gcloud auth print-access-token)
在任何操作之前,验证当前身份是否为服务账户:
bash
gcloud config list --format=text(core.account,core.project)
如果当前账户不是服务账户(例如,不以gserviceaccount.com结尾),请停止操作并要求用户切换凭据后再继续。
安全建议:
- - 使用专用的最低权限服务账户进行自动化任务。切勿使用个人或管理员账户。
- 在沙箱或开发项目中测试,然后再对生产环境运行命令。
- 在执行命令前,使用gcloud config list验证当前项目。
- 令牌大约1小时后过期——如果遇到401未授权错误,请重新生成。
- 令牌继承已认证账户的所有权限,包括对敏感数据的读取权限。
- 如果怀疑存在未授权访问,立即撤销令牌:gcloud auth revoke
- 通过查看项目的Cloud Audit Logs定期审计活动。
安全注意事项
重要提示: 此技能可以以与已认证Google Cloud账户相同的权限访问Firestore数据。为安全起见,此技能要求在执行任何操作(包括只读操作)前获得用户明确批准。
为降低风险:
- 1. 仅对具有最低必要Firestore权限的服务账户使用此技能
- 对开发/测试和生产环境使用不同的项目
- 在允许任何操作前,查看gcloud config list的输出
- 仅授予roles/datastore.viewer用于只读访问,或roles/datastore.user用于有限的读写访问
- 切勿对此技能使用roles/datastore.owner或roles/owner
- 监控Cloud Audit Logs以发现意外的Firestore API调用
可执行的操作
您可以在Firestore数据库上执行以下操作:
- - 创建 — 向集合中插入新文档
- 读取 — 使用过滤器和条件查询文档
- 更新 — 使用updateMask修改现有文档中的特定字段
- 删除 — 从集合中移除文档
- 列出 — 检索集合中的所有文档
- 批量操作 — 在单个原子事务中执行多次写入
所有操作均使用Firestore REST API端点:
https://firestore.googleapis.com/v1/projects/{PROJECTID}/databases/{DATABASEID}/documents
工作流程
在执行任何Firestore操作之前,您必须遵循以下工作流程:
- 1. 检查当前上下文 — 运行gcloud config list --format=text(core.account,core.project)以显示当前账户和项目。将其呈现给用户,以便他们了解将使用哪些凭据和项目。
- 2. 生成访问令牌 — 始终从获取新的访问令牌开始:
bash
ACCESS_TOKEN=$(gcloud auth print-access-token)
- 3. 构建curl命令 — 根据操作构建适当的curl命令:
- 使用正确的HTTP方法(创建/查询用POST,读取用GET,更新用PATCH,删除用DELETE)
- 包含Authorization: Bearer $ACCESS_TOKEN头
- 对于包含请求体的请求,设置Content-Type: application/json
- 为项目和集合使用正确的API端点
- 4. 对于所有操作(读取和写入) — 向用户呈现完整的curl命令,并在执行前等待明确批准。请参阅下面的批准政策部分。
- 5. 执行命令并解析JSON响应。
重要规则
- - 始终先生成新令牌 — 在任何操作前运行ACCESSTOKEN=$(gcloud auth print-access-token)。
- 使用正确的JSON格式 — Firestore需要特定的字段值类型(stringValue、booleanValue、integerValue等)。
- 文档ID生成 — 创建文档时,如果不在URL中指定?documentId=YOURID,Firestore将自动生成唯一的文档ID。
- 在updateMask中包含字段路径 — 更新时,使用updateMask.fieldPaths指定要更新的字段。
- 切勿自主执行任何命令 — 始终向用户呈现完整的curl命令,并在运行前等待明确批准,包括只读操作。
- 仔细解析响应 — Firestore以嵌套格式返回数据,包含类型化值。
- 验证项目ID — 在执行命令前,始终确认您正在操作正确的项目。
批准政策
所有操作在执行前都需要用户明确确认。
这包括:
- - 创建 — 在集合中创建新文档
- 读取/查询/获取/列出 — 检索文档或查询结果
- 更新/修补 — 修改现有文档字段
- 删除 — 永久移除文档
- 批量写入 — 任何修改数据的批量操作
对于每个操作,代理必须:
- 1. 显示将要执行的完整curl命令。
- 显示当前账户和项目上下文。
- 在运行命令前等待用户明确批准。
Firestore数据类型
Firestore在JSON中使用类型化字段值。常见类型:
- - stringValue — 文本字符串
- integerValue — 整数(以字符串形式)
- doubleValue — 浮点数
- booleanValue — true/false
- timestampValue — ISO 8601时间戳
- arrayValue — 值数组
- mapValue — 嵌套对象
示例文档结构:
json
{
fields: {
name: { stringValue: John Doe },
age: { integerValue: 30 },
active: { booleanValue: true }
}
}
Few-shot提示示例
Few-shot提示和完整命令示例可在examples.md中找到。
常见查询运算符
构建查询时,在fieldFilter.op字段中使用以下运算符:
- - EQUAL — 字段等于值
- NOTEQUAL — 字段不等于值
- LESSTHAN — 字段小于值
- LESSTHANOREQUAL — 字段小于或等于值
- GREATERTHAN — 字段大于值
- GREATERTHANOREQUAL — 字段大于或等于值
- ARRAYCONTAINS — 数组字段包含值
- IN — 字段值在提供的数组中
- ARRAYCONTAINSANY — 数组字段包含提供的任意值
故障排除
专门的故障排除指南,请参阅troubleshooting.md。