@cocreate/api

中文文档
2.1.2发布于 2026/11/07GPL-3.0ES Module近 30 天下载 2530

A secure, multi-tenant API execution gateway and proxy layer that dynamically resolves endpoint actions, processes AST micro-operators, handles webhooks, and manages third-party integrations with strict tenant environment isolation.

原始使用文档
文档目录34 节
  1. Documentation
  2. Table of Contents
  3. Features
  4. Dynamic Operations Sandbox
  5. Installation
  6. Usage
  7. Client Usage: HTML5 Attributes
  8. Declare the Provider
  9. Build a Request
  10. Receive a Response
  11. Chain the Result
  12. Choose the Trigger
  13. Scope and Endpoints
  14. Render Errors
  15. Client Usage: JavaScript API
  16. Register a Provider
  17. Execute a Request
  18. Work With the Payload Directly
  19. Custom Handlers
  20. Server Usage
  21. Module Initialization
  22. Direct Programmatic API Execution
  23. Attribute Reference
  24. Notes
  25. How it Works
  26. Architecture and Payload Specs
  27. Unified Payload Object Schema
  28. Context Engine Parameter Schema
  29. Security Implementations
  30. Context Sandboxing
  31. Timing-Safe Signature Verification
  32. How to Contribute
  33. License
  34. HTTP entrance

@cocreate/api

A dynamic, multi-tenant API execution gateway, proxy engine, and webhook orchestration routing layer. It securely manages distributed integration architectures, allowing applications to configure, evaluate, and execute complex third-party API configurations and multi-step macro workflows on the fly. By coupling an Abstract Syntax Tree (AST) evaluation sandbox (NativeSandbox) with strict tenant environmental isolation, it transforms declarative configurations into low-latency outbound integrations and secure inbound webhooks without introducing cross-tenant data leaks.

The package ships both halves of the integration. On the client, third-party APIs are consumed declaratively through HTML5 attributes or through an equivalent JavaScript API. On the server, the gateway resolves credentials, evaluates configurations, and executes the outbound request, so API keys never reach the browser.


Documentation

For complete API references, integration guides, webhook configuration, and usage examples, visit the CoCreate API Documentation.

Table of Contents


Features

  • Abstract Syntax Tree (AST) Micro-Operators: Evaluates functional configuration contexts inline, supporting expressive syntax tags (like $data, $event, $actions, and $request) along with custom business logic operators.
  • Declarative Client Interface: Configures complete request and response cycles using HTML5 attributes, assembling payloads from form fields and mapping results back into the document without provider-specific code.
  • Tenant Environmental Isolation Firewall: Guarantees cryptographic data partitioning by intercepting execution matrices and forcefully injecting bound organization_id and verified host values into core operations.
  • Real-Time WebSocket Integration Matrix: Seamlessly hooks into live server networks (wsManager), listening for incoming api and endpoint actions and streaming real-time operational state payloads directly back to the client.
  • Dual Outbound Execution Vectors: Wire-speeds native, pre-constructed HTTP payloads instantly, or dynamically builds custom multi-step outbound integrations by resolving runtime metadata against persistence layers.
  • Cryptographic Signatures & JWT Orchestration: Features native, low-latency hooks to generate JSON Web Tokens ($jwt) using HMAC-SHA256, alongside timing-attack-safe webhook signature verification (verifySignature).
  • Sealed AST Runtime Sandbox: Features a hardened utility matrix containing sandboxed global objects (JSON, Array, Math) and helper methods (like Base64 encoding/decoding and URL encoders), safely wrapping calculations outside the global scope.
  • Native Edge Stream Inspection: Inspects active server response buffers automatically, failing back to a clean 200 OK only if macro pipelines didn't forcefully complete or close the stream.

Dynamic Operations Sandbox

The engine evaluates standard properties and custom functional variables using a predefined sandbox layer. This allows configuration profiles to extract fields safely on the fly.

Core Operator ScopeExtracted Source Value
$dataExtracts standard key parameters from the active input execution payload (data).
$eventParses raw incoming JSON parameters transmitted via active webhook streams.
$actionsExposes an array of historical execution artifacts from previous macro pipeline links.
$request / $headerExposes underlying HTTP request properties, methods, and incoming network parameters.
$jwtTriggers an active cryptographic signing loop, building a secure JWT token from properties.

Installation

npm install @cocreate/api

Usage

The API engine is consumed two ways. On the client, integrations are declared entirely with HTML5 attributes or driven through the JavaScript API. On the server, the gateway resolves credentials, evaluates configurations, and performs the outbound request.


Client Usage: HTML5 Attributes

The client requires no provider-specific code. Declaring a provider on a container registers it, and every element carrying that provider's attributes contributes a field to the outbound payload or receives a field from the response.

Declare the Provider

<form api="stripe"></form>

The value of api is the provider name. It becomes the attribute prefix used by every field, the action name used to trigger the request, and the payload key sent over the socket. module is accepted as an alias.

Build a Request

Each participating element declares the method it belongs to and the path it populates.

<form api="stripe">
    <input stripe="customers.create" stripe-key="email" />
    <input stripe="customers.create" stripe-key="metadata.workspace" value="production" hidden />

    <button actions="stripe.customers.create">Create customer</button>
</form>

Clicking the button collects every [stripe="customers.create"] element in the form, reads each value, and assembles the payload from the stripe-key paths:

{
    "method": "stripe.customers.create",
    "stripe": {
        "email": "[email protected]",
        "metadata": { "workspace": "production" }
    },
    "status": "await"
}

Keys use dot notation for nesting and bracket notation for indexes, so messages[0].content and metadata.workspace both resolve to nested objects. A key ending in [] appends to an array, allowing several elements to contribute entries to the same collection.

Receive a Response

An element marked -request="false" is excluded from the outbound payload and used only to receive a value from the response.

<input
    stripe="customers.create"
    stripe-key="id"
    stripe-request="false"
    hidden />

When the response arrives, the value at that path is written into the element. Use -response="false" for the inverse: a field that is sent but never overwritten. A key of {} maps the entire provider payload rather than a single path.

Chain the Result

Because the response is written with the standard value pipeline, any binding module can react to it.

<input
    stripe="customers.create"
    stripe-key="id"
    stripe-request="false"
    input-query="$document #customer-id"
    input.value="$value"
    hidden />

Choose the Trigger

By default a request runs only when the action fires. -event opts the element into additional triggers.

<input
    currency="rates.read"
    currency-key="base"
    currency-event="input, onload" />

Available trigger types are action, input, onload, nodeObserver, and attributeObserver.

Scope and Endpoints

Fields are collected from the nearest ancestor form, or from the document when the element is not inside one, so several independent integrations can coexist on one page. Elements inside a [template] are skipped, keeping templates out of live payloads.

An endpoint attribute selects the configured route, and a query attribute marks the request as a query.

<input stripe="customers.create" endpoint="POST /v1/customers" hidden />

Render Errors

When the gateway returns an error, the client renders it into any template matching the provider name, receiving type, method, status, and message.

<div template="stripe-error">{{message}}</div>

Client Usage: JavaScript API

The same pipeline is available programmatically for providers that need registration options, custom handlers, or imperative execution.

Register a Provider

import api from "@cocreate/api";

api.init({
    name: "stripe",
    endPoints: {},
    options: { socket: true }
});

Calling init() without arguments scans the document for [api] and [module] elements and registers each provider it finds. Registration also creates the socket connection unless options.socket is false.

Execute a Request

api.request({
    name: "stripe",
    method: "customers.create",
    form: document.querySelector("#billing"),
    type: "action"
});

Work With the Payload Directly

const data = await api.getData({
    name: "stripe",
    method: "customers.create",
    form: document.querySelector("#billing")
});

await api.send({ name: "stripe", method: "customers.create", data });

send() transmits over the CoCreate socket as provider.method with the payload under the provider key, awaiting the correlated response before mapping it back into the document.

Custom Handlers

If a registered module defines a handler for a method, its request and response functions take precedence over the default collection and mapping behavior. This is the extension point for providers needing bespoke serialization, such as third-party SDK elements.


Server Usage

Module Initialization

Bind the API framework directly during the bootstrapping phase of your core CoCreateServer application matrix.

import apiGateway from '@cocreate/api';
import { CoCreateServer } from '@cocreate/cluster';

// Boot the primary application server instance
const serverContext = await CoCreateServer.start();

// Initialize the API engine and register WebSocket listeners
await apiGateway.init(serverContext);

Direct Programmatic API Execution

Execute complex third-party outbound macro operations using unified integration objects.

import apiGateway from '@cocreate/api';

// The method identifies the provider credentials, and the endpoint resolves the data model
const payload = {
  organization_id: "64b9a32e18f21bc56789abcd",
  method: "stripe",
  endpoint: "POST /v1/customers",
  stripe: {
    email: "$data.userEmail",
    metadata: {
      workspace: "production"
    }
  },
  userEmail: "[email protected]"
};

const result = await apiGateway.executeApi(payload);

Attribute Reference

name refers to the provider declared by the api attribute, for example stripe or openai.

AttributeRole
api="name"Declares and registers the provider. module is an accepted alias.
name="method"Marks the element as a participant in that method's request and response.
name-key="path"Path the element populates or receives. Supports dot and bracket notation, [] to append to an array, and {} for the entire payload.
name-event="types"Trigger types that perform a request: action, input, onload, nodeObserver, attributeObserver. Defaults to action.
name-request="false"Excludes the element from the outbound payload, making it response-only.
name-response="false"Excludes the element from response mapping, making it request-only.
endpoint="METHOD /route"Sets the endpoint resolved by the gateway.
queryFlags the request as a query.
template_id="id"Renders the response through the matching template instead of setting a value.
actions="name.method"Triggers the request, and participates in action chains.

Notes

  • Fields are gathered from the nearest ancestor form, falling back to the document.
  • Elements inside a [template] are excluded from payload collection.
  • Elements carrying an actions attribute are skipped during response mapping so that chained behavior is not overwritten.
  • Requests are sent with broadcasting disabled, so a payload is never echoed to other clients.

How it Works

  1. Bootstrap & Event Binding: During init(), the module hooks directly into the server's WebSocket framework (wsManager), capturing incoming api and endpoint event signals to route them to the execution handler.
  2. Context and Firewall Initialization: For every outbound execution, the engine evaluates the parameters against a parent context firewall. Security profiles match inbound connections against verified socket bounds, locking organization_id and host configurations.
  3. Macro Configuration Resolving: The engine sends queries through the CRUD synchronization layer to match configuration files against stored endpoints or active integration details inside the database collection.
  4. AST Matrix Processing: The router recursively scans headers, parameters, and bodies via processOperators(). Found strings matching active variables (such as $ prefixes) are securely compiled in the isolated NativeSandbox environment.
  5. Outbound Network Fetching: The system builds the request profile, forcefully sets content headers, maps an AbortController timeout circuit breaker (15-second cutoff), and executes the request using the runtime's native fetch implementation.
  6. Payload Delivery & Webhook Dispatching: Webhook targets matching inbound edge rules validate security values, execute configured workflows sequentially, and return structured responses.

Architecture and Payload Specs

Unified Payload Object Schema

The API engine relies on flat, declarative payload objects passed through the execution pipeline.

Field ElementTypeRole
organization_idStringRequired. Targets the tenant profile used to resolve integrations and credentials.
methodStringRequired. Provider identity (for example "stripe").
endpointStringRequired. HTTP method and route used to locate the configured endpoint (for example "POST /v1/customers").
[providerName]ObjectProvider-specific input object (for example stripe) that is processed through the AST engine.

Context Engine Parameter Schema

The API engine creates an execution context for every request.

Field ElementTypeRole
dataObjectInput payload supplied to the execution pipeline.
eventObjectParsed webhook request body.
actionsArrayResults accumulated from previous workflow steps.
organization_idStringTenant identifier.
hostStringVerified tenant host used for isolation.
crud / wsManagerObjectReferences to the CRUD layer and WebSocket manager.

Security Implementations

[!WARNING] Parent execution context always overrides incoming payload values to prevent cross-tenant access or privilege escalation.

Context Sandboxing

Internal services such as crud and wsManager intercept execution arguments and automatically enforce the authenticated tenant context. Even if a malicious configuration attempts to override tenant information, the runtime replaces it with the verified execution context.

Timing-Safe Signature Verification

Inbound webhooks configured with authentication rules are validated using crypto.timingSafeEqual, preventing timing attacks during signature verification.


How to Contribute

We encourage contributions to all CoCreate libraries. Please see our CONTRIBUTING.md guide for development workflows and contribution guidelines.

If you discover a bug or would like to request a feature, please open an issue on our GitHub Issues tracker.

For complete API documentation, integration examples, and webhook guides, visit:

https://cocreatejs.com/docs/api


License

This software is dual-licensed under the GNU Affero General Public License version 3 (AGPLv3) and a commercial license.

  • Open Source Use: For open-source projects and non-commercial use, this software is available under the AGPLv3. For the full license text, see the LICENSE file.
  • Commercial Use: Commercial users must obtain a commercial license. Commercial licenses are available when signing up for an API key through the CoCreate platform.

HTTP entrance

The initialized API service exposes http(req, res) alongside webhook(req, res). The module accepts POST JSON bodies up to 1 MiB, resolves organization scope from the hostname, and authorizes through server.authorize.request before execution. Bearer API keys and the envelope apikey field are supported; conflicting keys are rejected. Missing permission or authorization service returns 403. Caller scope and socket/user identity are excluded, and execution uses the sanitized envelope without the transport key.

send() remains the execution API used by trusted internal calls and socket dispatch, where socket authorization already occurs. The existing webhook AST owns provider signature verification.

版本与发布记录

@cocreate/api 共发布 277 个版本,首次发布于 2021/01/24;过去 12 个月发布了 15 个版本。

版本发布日期距今
2.1.2latest2026/11/073 天前
2.1.12026/11/073 天前
2.1.02026/11/064 天前
2.0.02026/11/037 天前
1.34.02026/10/2120 天前
1.31.02026/08/043 个月前

关于 @cocreate/api 的常见问题

@cocreate/api 是什么?

A secure, multi-tenant API execution gateway and proxy layer that dynamically resolves endpoint actions, processes AST micro-operators, handles webhooks, and manages third-party integrations with strict tenant environment isolation.

如何安装 @cocreate/api?

在项目目录运行 npm install @cocreate/api,或使用 pnpm add / yarn add / bun add。

@cocreate/api 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/cocreate__api 是否存在。

@cocreate/api 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@cocreate/api 可以商用吗?

@cocreate/api 使用 GPL-3.0 许可。强 Copyleft:分发包含该代码的软件时,整个作品需以 GPL-3.0 兼容许可开源,并附带专利与反锁定条款。仅内部使用不触发此义务。以上为概括,不构成法律意见,请以许可原文为准。

@cocreate/api 的下载量有多少?

根据 npm 官方下载统计,@cocreate/api 在 2026/10/10 – 2026/11/08 这 30 天内被下载 2,530 次,日均约 84 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@cocreate/api 还在维护吗?

最新版本 2.1.2 发布于 2026/11/07(3 天前);至今共发布 277 个版本,其中过去 12 个月发布了 15 个;首次发布于 2021/01/24。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。