Developer docs / Plug-ins
Building a plug-in
Write a plug-in with @commernauts/plugin, build one .plugin.json, try it on your sandbox store, publish it in Studio.
How a plug-in runs
Commernauts runs your plug-in, a small Worker, for every store that installs it. When an event it subscribes to happens in a store, the platform sends it the event and the store's settings for it, and the plug-in answers with actions. The platform checks each action against the permissions the owner granted and applies it through the store's API.
A plug-in never holds a store credential, and one version runs for every store that installed it.
Start a project
npx --registry=https://npm.commernauts.com @commernauts/plugin new my-plugin
cd my-plugin && npm install
npm run build # dist/my-plugin.plugin.jsonThe project has two parts: commernauts.plugin.ts says what the plug-in is and asks for, and src/index.ts is its code. Its one dependency is @commernauts/plugin. npm run build runs commernauts-plugin build, which checks the manifest, bundles the code and writes the package to dist/.
@commernauts/plugin comes from the Commernauts registry, npm.commernauts.com, and is not on the public npm registry. The new project has an .npmrc that points at it, so npm install works with no setup. The build tools it installs come from the same registry, from a pinned and reviewed mirror; other packages are not served.
The manifest: commernauts.plugin.ts
import { defineManifest, Event, Permission } from '@commernauts/plugin';
export default defineManifest({
name: 'order-notes',
permissions: [Permission.OrdersRead, Permission.OrdersWrite],
events: [Event.OrderCreated],
settings: [
{ key: 'text', label: 'Note on each new order', type: 'text', required: true },
],
});- permissions: what the person installing grants. They must hold plugins.install and every permission listed here.
- events: what the plug-in runs on. Each event needs its read permission; defineManifest makes a missing one a type error.
- capabilities and ui: shipping rates, an address for carriers, buttons on the order page.
- settings: the fields the owner fills in when installing, and can change later.
Name every value with Event, Permission, Capability and Action from @commernauts/plugin; defineManifest checks them as you type. The marketplace checks against the same lists, so what your editor accepts is what review accepts.
A commernauts.plugin.json works too. Add "$schema": "./node_modules/@commernauts/plugin/plugin.schema.json" at the top and your editor autocompletes and checks it the same way. npm run build checks either one.
The code
import { definePlugin, Event, Action } from '@commernauts/plugin';
import manifest from '../commernauts.plugin.ts';
export default definePlugin(manifest, {
[Event.OrderCreated]: (event, { install }) => {
const order = event.data.order;
const text = install.settings.text
.replaceAll('{name}', order.customer_name || 'there')
.replaceAll('{order}', String(order.order_number));
return [{ type: Action.OrderNote, order_id: order.id, text }];
},
});Each handler gets the event and the install (its id, the store id and the owner's settings) and returns a list of actions, or nothing. With the manifest passed in, every event it lists needs a handler.
Try it on your sandbox store
- In Studio, Publish a plug-in and upload dist/<name>.plugin.json (up to 1 MB).
- Open the listing and install your draft on your sandbox store. Only your stores can install it before review.
- Place a test order on the sandbox store, then look at the order's timeline and the plug-in's Recent runs in the store's Admin, Store version.
Publish and update
- Once a reviewer approves it, every store can install it.
- Publish fixes and features with New version. Stores are offered the update.
- A version that asks for more permissions than the one a store runs waits until that store's owner allows them.