> ## Documentation Index
> Fetch the complete documentation index at: https://docs.launchmystore.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Test a Function

> Execute a pre-compiled function in an ephemeral sandbox without installing it.

`POST /apps/functions/test` runs a **pre-compiled WASM module** in the same
sandboxed worker the production dispatcher uses — same per-type timeout,
same output validator — but **nothing is persisted**. No function-run
record, no state change on any installation.

The server does **not** compile JavaScript. You compile your function
locally with `lms function build` (Javy dynamic mode), then send the
resulting WASM bytes as Base64. Static-mode modules are rejected.

Use it for:

* Quick iteration in the developer dashboard's "Try it" editor.
* CI smoke-tests that exercise a compiled function against a known fixture.
* Schema-conformance checks against the type-specific output validator.

**Auth:** merchant JWT. The sandbox is rate-limited per
calling account; abusive callers (sustained timeouts) are throttled.

***

## Request

<ParamField body="wasmBase64" type="string" required>
  Base64-encoded WASM bytes, produced locally via `lms function build`
  (Javy dynamic mode). Max decoded size is enforced server-side
  (\~700 KB of Base64). Static-mode modules are rejected.
</ParamField>

<ParamField body="type" type="string" required>
  Function type — determines the input shape that will be passed and the
  output validator that will check the return value. One of:
  `discount`, `shipping_rate`, `payment_customization`,
  `delivery_customization`, `order_validation`, `cart_transform`,
  `fulfillment_constraints`, `local_pickup_options`, `pickup_point_options`.
</ParamField>

<ParamField body="input" type="object" required>
  Test input passed to the function. Should match the input contract for
  the function type — see [Function input fields](/functions/input-fields).
</ParamField>

<ParamField body="timeoutMs" type="integer">
  Execution timeout in ms. Server-clamped to the per-type cap below
  (range: 50–5000). Omit to use the per-type default.
</ParamField>

### Per-type timeouts

| Function type             | Default timeout | Hard cap |
| ------------------------- | --------------- | -------- |
| `discount`                | 500ms           | 500ms    |
| `payment_customization`   | 500ms           | 500ms    |
| `order_validation`        | 1000ms          | 1000ms   |
| `cart_transform`          | 1000ms          | 1000ms   |
| `delivery_customization`  | 1000ms          | 1000ms   |
| `fulfillment_constraints` | 1000ms          | 1000ms   |
| `local_pickup_options`    | 1500ms          | 1500ms   |
| `shipping_rate`           | 2000ms          | 2000ms   |
| `pickup_point_options`    | 2000ms          | 2000ms   |

Memory cap is **128 MB** per execution. Output is capped at **20 KB**
serialized — larger outputs fail validation. See
[Functions overview](/functions/overview) for the full runtime spec.

***

## Example: discount

```bash theme={null}
curl -X POST "https://api.launchmystore.io/apps/functions/test" \
  -H "Authorization: Bearer <MERCHANT_JWT>" \
  -H "Content-Type: application/json" \
  -d '{
    "type": "discount",
    "wasmBase64": "<BASE64_ENCODED_WASM_BYTES>",
    "input": {
      "cart": {
        "cost": { "subtotalAmount": { "amount": 75, "currencyCode": "USD" } },
        "lines": [
          { "id": "line_1", "quantity": 3, "cost": { "amountPerQuantity": { "amount": 25, "currencyCode": "USD" } } }
        ]
      }
    }
  }'
```

## Response

A successful or failed **execution** both return `200 OK` — the outcome is
encoded in the `data` body (`success: true/false`). The envelope is the
standard `{ status, state, message, data }` shape (`state: "success"` for a
completed execution). Only auth / bad-request / quota errors surface as
non-200, as a `{ status, state: "error", message, data }` envelope.

<ResponseField name="data.success" type="boolean">`true` when the function ran AND its output passed validation.</ResponseField>
<ResponseField name="data.output" type="object">The function's return value. On a validation failure this is the raw output that failed; `null` on a hard runtime failure.</ResponseField>
<ResponseField name="data.executionTimeMs" type="integer">Wall-clock execution time inside the WASM worker.</ResponseField>
<ResponseField name="data.wasmSize" type="integer">Decoded WASM module size in bytes.</ResponseField>
<ResponseField name="data.error" type="string">Runtime/timeout error message. Present only when `success: false`.</ResponseField>
<ResponseField name="data.validationError" type="string">Output validator error message — present when the function returned but its output shape was wrong.</ResponseField>

### Example success

```json theme={null}
{
  "status": 200,
  "state": "success",
  "message": null,
  "data": {
    "success": true,
    "output": {
      "discounts": [
        {
          "targets": [{ "orderSubtotal": {} }],
          "value": { "percentage": { "value": 10 } },
          "message": "10% off $50+"
        }
      ]
    },
    "executionTimeMs": 4,
    "wasmSize": 3014
  },
  "count": null,
  "pagination": null
}
```

### Example timeout

```json theme={null}
{
  "status": 200,
  "state": "success",
  "message": null,
  "data": {
    "success": false,
    "error": "WASM execution timed out after 500ms",
    "validationError": null,
    "output": null,
    "executionTimeMs": 500,
    "wasmSize": 3014
  },
  "count": null,
  "pagination": null
}
```

### Example validation error

```json theme={null}
{
  "status": 200,
  "state": "success",
  "message": null,
  "data": {
    "success": false,
    "error": null,
    "validationError": "discount: each discount value must set exactly one of `percentage` or `fixedAmount`",
    "output": {
      "discounts": [{ "targets": [{ "orderSubtotal": {} }], "value": {} }]
    },
    "executionTimeMs": 3,
    "wasmSize": 3014
  },
  "count": null,
  "pagination": null
}
```

***

## Error codes (non-200)

Non-200 responses use the same envelope with `state: "error"` and a human
message, e.g.:

```json theme={null}
{
  "status": 400,
  "state": "error",
  "message": "Unknown function type: foo",
  "data": null
}
```

| Code  | `message`                                 | When                                            |
| ----- | ----------------------------------------- | ----------------------------------------------- |
| `400` | `Unknown function type: <type>`           | `type` is not one of the nine function types.   |
| `400` | `Invalid base64 in wasmBase64: ...`       | `wasmBase64` is not valid Base64.               |
| `400` | validation reason from the WASM validator | bytes are not a valid Javy dynamic-mode module. |
| `401` | —                                         | Missing or invalid merchant JWT.                |
| `429` | —                                         | Sandbox rate limit hit. Wait and retry.         |
