| Crates.io | shopify_function_wasm_api |
| lib.rs | shopify_function_wasm_api |
| version | 0.3.0 |
| created_at | 2025-05-20 18:31:18.113435+00 |
| updated_at | 2025-09-10 19:42:17.863363+00 |
| description | High-level interface for interfacing with the Shopify Function Wasm API |
| homepage | https://github.com/Shopify/shopify-function-wasm-api |
| repository | https://github.com/Shopify/shopify-function-wasm-api |
| max_upload_size | |
| id | 1681938 |
| size | 84,980 |
The Shopify Wasm API provides functions for your Shopify Function to read and write data. This API facilitates communication between the Shopify platform (host) and the guest Wasm module, primarily through a set of imported functions that the Wasm module can call. The following sections describe the value representation format, along with status codes, error codes, and the NanBoxed value structure used by the API.
Input values in the Wasm API are represented as 64-bit NaN-boxed
values, represented as i64 constants in WebAssembly. NaN-boxing
provides a performant way to represent multiple value types (such as
numbers, strings, booleans, objects, arrays, or errors) within
64-bits, without requiring additional memory allocations for type
information. The API uses specific bit patterns within the 64 bits
to encode value information.
63 62 52 51 50 49 46 45 32 31 0
+---+------------+--+--+--------+-----------+--------------------+
| 0 | 11111111111| 1 | 1 | TTTT | LENGTH | VALUE |
+---+------------+--+--+--------+-----------+--------------------+
^ ^ ^ ^ ^ ^
Sign Exponent Quiet Tag bits Length Value bits
(0) (all 1s) NaN (type) (14 bits) (32 bits - data/ptr)
When a value is a floating-point number (type tag 2), it is not
encoded through NaN-boxing. Instead, it directly uses the
standard IEEE 754 double-precision binary floating-point format:
63 62 52 51 0
+---+------------+--------------------------------------------+
| S | Exponent | Mantissa |
+---+------------+--------------------------------------------+
^ ^ ^
Sign Exponent Mantissa
(variable) (variable) (variable)
Floating point numbers in our API follow the IEEE-754 specification.
The Tag bits (TTTT) in the NanBox structure determine the logical
type of the i64 value. The following type tags are used:
Null - Null valueBool - Boolean value (true/false)Number - Numeric value (f64)String - UTF-8 encoded string (pointer + length)Object - Key-value collection (pointer + length)Array - Indexed collection of values (pointer + length)Error - Read error codesTo read input data provided by the Shopify platform, your Wasm module will use a set of imported API functions. These functions allow you to access the root input value and traverse complex data structures like objects and arrays. For a complete list and detailed signatures of these read functions, refer to the C header file or the WebAssembly Text Format definition.
Each read operation that retrieves data typically returns a NaN-boxed value. This value should be interpreted according to the NanBox structure and Value Types detailed above.
When a 64-bit NaN-boxed value has its type tag bits set to 15
(Error), it signifies that a read error occurred. The lower 32 bits of
this i64 (the "Value field" in the NanBox structure) will then
contain one of the following i32 error codes:
DecodeError - Value could not be decodedNotAnObject - Expected an object but received another typeByteArrayOutOfBounds - Byte array index out of boundsReadError - Error occurred during readingNotAnArray - Expected an array but received another typeIndexOutOfBounds - Array index out of boundsNotIndexable - Value is not indexable (not an object or array)Shopify does not consider additions to this list to be a breaking change, so developers are encouraged to handle the case that new error values are emitted.
To write output data back to the Shopify platform, your Wasm module will use a corresponding set of imported API functions. These functions allow you to construct complex data structures, such as objects and arrays, and populate them with various value types (strings, numbers, booleans, null) as defined in the Value Types section. For a complete list and detailed signatures of these write functions, refer to the C header file or the WebAssembly Text Format definition.
Most write operations return an i32 status code. A value of 0
(Success) indicates the operation was successful, while other values
signify errors.
These are the i32 status codes returned by write operations:
Success - The operation was successfulIoError - Error occurred during writingExpectedKey - Expected a key but received a valueObjectLengthError - Object length mismatchValueAlreadyWritten - Value already writtenNotAnObject - Expected an object but received another typeValueNotFinished - Value creation not completedArrayLengthError - Array length mismatchNotAnArray - Expected an array but received another typeShopify does not consider additions to this list to be a breaking change, so developers are encouraged to handle the case that new status codes are emitted.