Search Documentation

Search for a documentation page...

Scripts API

Run one JavaScript program on the server that calls many Store API operations in a single request, with the caller's own credential.

A script is a JavaScript program that calls Store API operations on the server. Use one when a job needs many calls: sweeping every page of a list, cross-referencing resources (every order that contains a product), updating a filtered set in bulk, or boiling large responses down to a few fields. For one lookup or one write, call the operation itself.

Run a script

POST /api/v1/scripts
FieldTypeRequiredDescription
codestringYesJavaScript source, up to 64 KB. Top-level await and return work
readOnlybooleanNoRefuse every operation that is not a GET. Defaults to false

Inside the program every operation is await tools.<operationId>(input). Path and query parameters are top-level fields of input. The JSON request body goes under body:

const product = await tools.getProduct({ idOrSlug: "classic-tee" });
if (product.status !== 200) return product;
return tools.updateProduct({ idOrSlug: "classic-tee", body: { name: "Classic Tee (organic)" } });

Each call resolves to { status, statusText, body }, non-2xx answers included, so check status before you trust body. Operation names are the operationIds in the OpenAPI spec.

This program reads every product in a collection and returns only the fields it needs:

curl -X POST \
-H "Authorization: Bearer sk_your_api_key" \
-H "Content-Type: application/json" \
-d @- https://your-store.yns.store/api/v1/scripts <<'EOF'
{
"readOnly": true,
"code": "const rows = [];\nfor (let offset = 0; ; offset += 100) {\n const page = await tools.listCollectionProducts({ idOrSlug: 'summer', limit: 100, offset });\n if (page.status !== 200) return page;\n rows.push(...page.body.data.map((p) => ({ id: p.id, name: p.name })));\n if (page.body.data.length < 100) break;\n}\nreturn rows;"
}
EOF

Response (200)

{ "result": [{ "id": "019…", "name": "Linen Shirt" }] }

result is the program's return value, or null.

Credentials and permissions

Every call in the program acts as the caller. It uses the same credential, so it reaches the same store, an API key keeps its scopes, and an OAuth connection keeps the team member's permissions. A call the caller could not make directly answers 403 inside the program.

Any store API key may run scripts, except a key limited to the storefront preset or the feedback scope. Any team member connected over OAuth may run them.

Limits

LimitValue
Runtime60 seconds
API calls100 per run
Time per call30 seconds
Concurrent calls8 at a time with Promise.all
Source64 KB
Result128 KB

The program runs in an isolated sandbox with plain JavaScript only. There is no fetch, no filesystem and no import. A script cannot call runStoreScript itself.

Operations a script can't call

Operations that need the merchant's approval are refused inside a script. The 400 names the operation, so you can call it directly after asking:

  • refunds, gift cards, wholesale orders and completing a draft order,
  • anything sent to customers,
  • team access and ownership,
  • every delete, remove and connect operation,
  • imports, unless they are a dryRun,
  • catalog reset, live payments, and changes to the store's currency, address or published state.

Writes are not rolled back

When a program fails partway, the calls it made before the failure have happened. Read the affected records before you run it again.

Errors

StatusDescription
400Invalid body, or the program failed: a syntax or runtime error, a limit, or a refused operation. error has the message
  • MCP Server – agents on the full server can run scripts; the directory versions leave them out