When to use this endpoint

Normally called after a movement by Stock_transaction, inventory transfer or imports. This records the supplied evidence; it does not perform or verify the movement.

Authentication and permissions

Authenticate with a signed API key or a logged-in CRM session on the account’s routed host. Signed requests act as the key owner. Session requests must include the dispatcher CSRF value when configured. All record identifiers belong to the authenticated tenant.

The dispatcher and these handlers do not impose an additional CRM2 Stock role or Stock TeamAccess record scope. CRM2 screen permissions are separate controls and must not be assumed to apply to CRM1. These routes do not become public because an individual legacy handler checks only the tenant context.

Parameters

ParameterTypeRequiredDescription
stockIDInteger-compatibleYesCatalogue ID. Required by the map, but active stock existence is not validated here.
type / qtyInteger-compatible / float-compatibleNo explicit required map flagsMovement type 0 In,1 Out,2 Allocate,3 Return,4 Damage,5 Transfer,6 OutAllocate,7 Import,8 OutRevert,9 WriteOff. Quantity is recorded, not applied.
fromID / fromType / fromQTY / fromsIDs, float balance or native {id,type}NoSource owner/type and its supplied balance snapshot. froms expands id/type. fromQTY is case-sensitive and is not qty.
toID / toType / toQTY / tosIDs, float balance or native {id,type}NoDestination owner/type and supplied balance snapshot. Type codes are stock-source types.
cdate / lastEdited / userIDUnix/parseable datetime / IDNoDefaults: microtime for cdate, current epoch for lastEdited, current actor for userID. Layout readonly flags do not prohibit supplied values.
meta / metaReference / metaInventoryID / metaReasonNative JSON / textNoMetadata or its projected keys. metaInventoryID is a legacy single inventory reference; metaReference is a display reference.
metaInventoryNative or JSON-encoded list of {id,qty}NoNative or JSON list of {id,qty}. id must be a nonnegative digit ID (0 is unbatched); qty must be finite and nonnegative. Every positive ID must be active and belong to stockID.

Example request

$history = FM_api('StockHistory_createData', ['stockID'=>'1042','type'=>'2','qty'=>'2.5','fromID'=>'0','fromType'=>'0','fromQTY'=>'10','toID'=>'1088','toType'=>'1','toQTY'=>'1','metaInventory'=>[['id'=>0,'qty'=>2.5]]]);

Example response

{"ok":1,"id":4081}

Examples show one representative result; database numeric scalars can be JSON strings. Application errors can use HTTP 200: inspect error and warning in the decoded response.

Errors and unusual responses

ResponseCondition
{"error":"not logged in"}No authenticated tenant context is available. Invalid signed credentials can be rejected by the dispatcher before the handler runs.
{"error":"missing or invalid parameter: stockID"}stockID omitted.
{"error":"invalid parameter: metaInventory"}Inventory value is not a decoded list.
{"error":"invalid inventory selection"}Malformed ID or nonfinite/nonnumeric qty.
{"error":"no inventory available"}A positive batch is missing/deleted or belongs to another stock.

Legacy application errors may be returned with HTTP 200. Check the decoded response for error, not only the HTTP status.

Behaviour and edge cases

  • Allowed map: id, stockID, stockReport, stock, type, fromID, fromType, fromQTY, froms, toID, toType, toQTY, tos, cdate, qty, lastEdited, userID, meta, metaReference, metaInventoryID, metaInventory and metaReason. Only stockID carries the business required flag. Source/destination balances, user and dates are caller evidence; no balance reconciliation or type allowlist is enforced.
  • If metaInventory contains positive IDs, reference text is rebuilt from matching inventory + storage rows as name(reference): qty followed by a literal backslash-n separator. Every positive ID is checked against active stock-owned inventory before insertion; missing or unrelated references fail.
  • Inventory ID 0 is the deliberate no-batch marker. Zero-only/empty lists do not trigger reference rebuilding. Keep metaInventory separate from inventory_id, allocation-row ID and stockID.
  • Explicit zero remains the no-batch marker. This writer validates newly recorded positive batch evidence; existing historical display text is not rewritten. Encoded/native list shapes and finite nonnegative batch quantities are required.

Side effects

  • Inserts stock_histories, invalidates that cache and writes generic field audit entries. No warehouse/container/inventory quantity change or movement event is performed.

See it in Fieldmotion

CRM1

No direct CRM1 browser caller was found. Legacy Stock_containerAdd does not create these movement-history rows.

CRM2

CRM2 movements call Stock_transaction (and transfers call StockInventory_transfer), which append this history server-side. The Layout stock-history create mapping also exposes it to generic helpers. Filter for the outer movement request in Network.

Related endpoints