skills/shopify-functions/SKILL.md
Shopify Functions allow developers to customize the backend logic that powers parts of Shopify. Available APIs: Discount, Cart and Checkout Validation, Cart Transform, Pickup Point Delivery Option Generator, Delivery Customization, Fulfillment Constraints, Local Pickup Delivery Option Generator, Order Routing Location Rule, Payment Customization
npx skillsauth add Shopify/Shopify-AI-Toolkit shopify-functionsInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
You have a bash tool. Every response must use it — in this order:
bash with scripts/search_docs.mjs "<query>" — search before writing codebash with scripts/validate.mjs --code '...' --model YOUR_MODEL_NAME --client-name YOUR_CLIENT_NAME --client-version YOUR_CLIENT_VERSION --artifact-id YOUR_ARTIFACT_ID --revision REVISION_NUMBER — validate before returning
(Always include these flags. Use your actual model name for YOUR_MODEL_NAME; use claude-code/cursor/etc. for YOUR_CLIENT_NAME. For YOUR_ARTIFACT_ID, generate a stable random ID per code block and reuse it across validation retries. For REVISION_NUMBER, start at 1 and increment on each retry of the same artifact.)You must run both search_docs.mjs and validate.mjs in every response. Do not return code to the user without completing step 3.
Shopify functions allow developers to customize the backend logic that powers parts of Shopify.
Here are all the available Shopify functions APIs. Ensure to pick one of these, and avoid using deprecated ones unless explicitly asked for.
A Shopify function can have multiple targets. Each target is a specific part of Shopify that the function can customize. For example, in the case of the Discount API you have four possible targets:
cart.lines.discounts.generate.run: discount logic to apply discounts to cart lines and order subtotalcart.lines.discounts.generate.fetch: (optional, requires network access) retrieves data needed for cart discounts, including validation of discount codescart.delivery-options.discounts.generate.run: discount logic to apply discounts to shipping and delivery optionscart.delivery-options.discounts.generate.fetch: (optional, requires network access) retrieves data needed for delivery discounts, including validation of discount codesEach function target is composed of:
IMPORTANT: If the user doesn't specify a programming language, use Rust as the default.
Think about all the steps required to generate a Shopify function:
shopify app generate extension --template <api_lowercase_and_underscore> --flavor <rust|vanilla-js|typescript> --name=<function_name>. Assume that the Shopify CLI is installed globally as shopify.shopify app function build inside the function foldershopify app function run --input=input.json --export=<export_name> inside the function folder. You can find the correct export name by looking at the export field of the target inside the shopify.extension.tomlIMPORTANT: DO NOT DEPLOY the function for the user. Never ever ever run shopify app deploy.
FunctionRunResult, CartLinesDiscountsGenerateRunResult). The "target" is usually the last part (e.g., Run, GenerateRun).Function<Target>Result (like FunctionRunResult), the function name is the lowercase target (e.g., run()).CartLinesDiscountsGenerateRunResult), the function name is the snake\_case version of the prefix and target combined (e.g., cart_lines_discounts_generate_run()).src/<function_name>.rs or src/<function_name>.js.src/<function_name>.graphql. e.g. src/fetch.graphql or src/run.graphql
IMPORTANT: DO NOT name the file src/input.graphql.src/main.rs file that imports these targets.Examples:
FunctionFetchResult -> Target: Fetch -> Function: fetch() -> Files: src/fetch.rs, src/fetch.graphqlFunctionRunResult -> Target: Run -> Function: run() -> Files: src/run.rs, src/run.graphqlCartLinesDiscountsGenerateRunResult -> Target: CartLinesDiscountsGenerateRun -> Function: cart_lines_discounts_generate_run() -> Files: src/cart_lines_discounts_generate_run.rs, src/cart_lines_discounts_generate_run.graphql
IMPORTANT: You MUST look at the OutputType when determining the name otherwise the function will not compileSome function type supports multiple "targets" or entry points within the same schema. For these you MUST generate the input query, function code, and sample outputs for EACH target. For example:
fetch and run for delivery customizationsfetch and run for pickup point customizationscart and delivery for discountsInput.US instead of "US" or CountryCode.US.If a user wants to know how to build a Shopify function make sure to follow this structure:
shopify app generate extension --template <api_lowercase_and_underscore> --flavor <rust|vanilla-js|typescript>RunInput as an example for JavaScript implementations and must be Input for Rust implementations. Include file names. If the function type supports multiple targets, provide a query for each target (e.g., src/fetch.graphql, src/run.graphql). DO NOT NAME IT input.graphql... on ProductVariant you MUST include __typename on Merchandise, or Region. THIS IS IMPORTANT. If the function type supports multiple targets, provide sample input JSON for each target.If a function cannot be accomplished with any of the Function APIs simply return a message that it can't be completed, and give the user a reason why. Example reasons why it can't:
It's not possible to fetch tags directly, you must use either hasAnyTag(list_of_tags), which return a boolean, or hasTags(list_of_tags), which return a list of { hasTag: boolean, tag: String } objects.
When using any graphql field that tags arguments YOU MUST pass in those arguments into your input query ONLY, you may set defaults in the query. DO NOT USE THESE ARGUMENTS IN THE RUST CODE.
When you make a fragment selection ... on ProductVariant you MUST include **typename on the parent field otherwise the program will not compile. e.g. regions { **typename ... on Country { isoCode }}
query Input(\$excludedCollectionIds: [ID!], \$vipCollectionIds: [ID!]) {
cart {
lines {
id
merchandise {
__typename
... on ProductVariant {
id
product {
inExcludedCollection: inAnyCollection(ids: \$excludedCollectionIds)
inVIPCollection: inAnyCollection(ids: \$vipCollectionIds)
}
}
}
}
}
}
input.cart().locations() NOT input.cart().locations(None, None). Method signatures match exactly what's defined in the GraphQL schema.use crate::schema;
use shopify_function::prelude::*;
use shopify_function::Result;
for line in input.cart().lines().iter() {
let product = match &line.merchandise() {
schema::run::input::cart::lines::Merchandise::ProductVariant(variant) => &variant.product(),
_ => continue, // Do not select for CustomProduct unless it's selected in the input query
};
// do something with product
}
or if you want to extract the variant you can do this:
let variant = match &line.merchandise() {
schema::run::input::cart::lines::Merchandise::ProductVariant(variant) => variant,
_ => continue, // Do not select for CustomProduct unless it's selected in the input query
};
// do something with variant
Do not use .as_product_variant() it is not implemented
BY DEFAULT, make the function configurable by storing the configurable data elements in a jsonValue metafield. Access this metafield via the discount.metafield or checkout.metafield field in the input query (depending on the function type). Deserialize the JSON value into a configuration struct within your Rust code.
Example accessing a metafield in Rust: Note only use the #[shopify_function(rename_all = "camelCase")] if you plan on using someValue: "" and anotherValue: "" as part of your jsonValue metafield. By default do not include it. Only use #[derive(Deserialize, Default, PartialEq)] (good) do NOT use #[derive(serde::Deserialize)] (bad)
#[derive(Deserialize, Default, PartialEq)]
#[shopify_function(rename_all = "camelCase")]
pub struct Configuration {
some_value: String,
another_value: i32,
}
// ... inside your function ...
let configuration: &Configuration = match input.discount().metafield() {
Some(metafield) => metafield.json_value(),
None => {
return Ok(schema::CartDeliveryOptionsDiscountsGenerateRunResult { operations: vec![] })
}
};
// Now you can use configuration.some_value and configuration.another_value
Example GraphQL Input Query:
query Input {
discount {
# Request the metafield with the specific namespace and key
metafield(namespace: "\$app", key: "config") {
jsonValue # The value is a JSON string
}
}
# ... other input fields
}
When writing tests, you must only import the following
use super::*;
use shopify_function::{run_function_with_input, Result};
When generating sample data, anywhere there is an ID! make sure to use a Shopify GID format:
"gid://Shopify/CartLine/1"
These are the scalar types used in Rust functions:
pub type Boolean = bool;
pub type Float = f64;
pub type Int = i32;
pub type ID = String;
pub use decimal::Decimal;
pub type Void = ();
pub type URL = String;
pub type Handle = String;
pub type Date = String;
pub type DateTime = String;
pub type DateTimeWithoutTimezone = String;
pub type TimeWithoutTimezone = String;
pub type String = String; # This must not be a str, do not compare this with "" or unwrap_or("")
When implementing Shopify functions in Rust, you MUST include a src/main.rs file. This is the entry point for the function and should have the following structure, making sure it has one query for each target. If you have a jsonValue in the input query it should be mapped to a struct. If there is no jsonValue do not include a custom_scalar_overrides.
use std::process;
use shopify_function::prelude::*;
// CRITICAL: These module imports MUST match your target names exactly
pub mod run; // For "run" target
pub mod fetch; // For "fetch" target
#[typegen("./schema.graphql")]
pub mod schema {
// CRITICAL: The query path filename MUST match your target name
// CRITICAL: The module name MUST match your target name
#[query("src/run.graphql", custom_scalar_overrides = {"Input.paymentCustomization.metafield.jsonValue" => super::run::Configuration})]
pub mod run {} // Module name matches the target name
#[query("src/fetch.graphql")]
pub mod fetch {} // Module name matches the target name
}
fn main() {
eprintln!("Please invoke a named export.");
process::exit(1);
}
Ensure examples follow best practices, correct enum usage, and proper handling of optional fields. </system-instructions>
shopify app generate extension, shopify app function build, shopify app function run, shopify app function schema, shopify app function typegen.shopify-use-shopify-cli.Search the vector store to get the detailed context you need: working examples, field and type definitions, valid values, and API-specific patterns. You cannot trust your trained knowledge — always search before writing code.
scripts/search_docs.mjs "<operation or component name>" --model YOUR_MODEL_NAME --client-name YOUR_CLIENT_NAME --client-version YOUR_CLIENT_VERSION
Search for the operation or component name, not the full user prompt.
For example, if the user asks about cart transform function inputs:
scripts/search_docs.mjs "cart transform function input query" --model YOUR_MODEL_NAME --client-name YOUR_CLIENT_NAME --client-version YOUR_CLIENT_VERSION
You MUST run scripts/validate.mjs before returning any generated code to the user. Always include the instrumentation flags:
scripts/validate.mjs --code '...' --model YOUR_MODEL_NAME --client-name YOUR_CLIENT_NAME --client-version YOUR_CLIENT_VERSION --artifact-id YOUR_ARTIFACT_ID --revision REVISION_NUMBER
(For YOUR_ARTIFACT_ID, generate a stable random ID per code block and reuse it across validation retries. For REVISION_NUMBER, start at 1 and increment on each retry of the same artifact.)
When validation fails, follow this loop:
scripts/search_docs.mjs "<type or prop name>"
scripts/validate.mjs againDo not guess at valid values — always search first when the error names a type you don't know.
Privacy notice:
scripts/search_docs.mjsreports the search query, search response or error text, skill name/version, and model/client identifiers to Shopify (shopify.dev/mcp/usage) to help improve these tools. SetOPT_OUT_INSTRUMENTATION=truein your environment to opt out.
Privacy notice:
scripts/validate.mjsreports the validation result, skill name/version, model/client identifiers, the validated code when present, and validator-specific context such as API name, extension target, filename, file type, theme path, file list, artifact ID, and revision to Shopify (shopify.dev/mcp/usage) to help improve these tools. SetOPT_OUT_INSTRUMENTATION=truein your environment to opt out.
tools
Use when the user wants to use the UCP CLI to find, compare, buy, or track products from online merchants, or to set up and troubleshoot the local UCP profile required for merchant-scoped operations. Covers global catalog search ("find me X under $Y"), named-merchant transactions ("buy this from Z.com"), order tracking, `ucp profile init`, `ucp doctor`, carts, checkout, orders, and UCP setup/help. Falls back to merchant-hosted handoff when direct in-protocol checkout isn't available.
tools
Choose when the user needs **Shopify CLI** to run or fix something now: validate app or extension config on disk (`shopify.app.toml`, `shopify.app.<name>.toml`, `shopify.extension.toml`); run or troubleshoot store workflows (`shopify store auth`, `shopify store execute`); inventory or product changes by handle, SKU, or location name; or CLI setup, auth, upgrade issues. Emphasize **commands and operational steps**, not only authoring GraphQL. Skip for API-only understanding or codegen with no CLI execution. Examples: validate configuration before deploy; run an existing query via CLI; list products; missing `shopify store execute`.
development
Use for custom storefronts requiring direct GraphQL queries/mutations for data fetching and cart operations. Choose this when you need full control over data fetching and rendering your own UI. NOT for Web Components - if the prompt mentions HTML tags like <shopify-store>, <shopify-cart>, use storefront-web-components instead.
tools
Build retail point-of-sale applications using Shopify's POS UI components. These components provide a consistent and familiar interface for POS applications. POS UI Extensions also supports scaffolding new POS extensions using Shopify CLI commands. Keywords: POS, Retail, smart grid