--- title: Discount Function Settings API description: >- The Discount Function Settings API lets you build UI extensions that provide custom configuration interfaces for discount functions. Use this API to create settings pages for Shopify Functions that implement discount logic. api_version: 2026-01 api_name: admin-extensions source_url: html: >- https://shopify.dev/docs/api/admin-extensions/latest/target-apis/contextual-apis/discount-function-settings-api md: >- https://shopify.dev/docs/api/admin-extensions/latest/target-apis/contextual-apis/discount-function-settings-api.md --- # Discount Function Settings API **Requires the \[function settings]\(/docs/api/admin-extensions/2026-01/web-components/forms/function-settings) component.:** The Discount Function Settings API lets you build UI extensions that provide custom configuration interfaces for [discount functions](https://shopify.dev/docs/apps/build/discounts/build-ui-extension). Use this API to create settings pages for [Shopify Functions](https://shopify.dev/docs/apps/build/functions) that implement discount logic. ### Use cases * **Discount configuration:** Build settings interfaces for discount Shopify Functions. * **Metafield storage:** Store discount function configuration in metafields with proper validation. * **Settings UI:** Create custom configuration experiences for discount logic and rules. * **Function integration:** Configure Shopify Functions that implement custom discount behavior. ### Support Targets (1) ### Supported targets * [admin.​discount-details.​function-settings.​render](https://shopify.dev/docs/api/admin-extensions/2026-01/targets/function-settings#discount-details-function-settings-) ## apply​Metafield​Change(**[change](#applymetafieldchange-propertydetail-change)**​) Updates or removes [metafields](https://shopify.dev/docs/apps/build/metafields) that store discount function configuration data. Accepts a change object with the operation type, key, namespace, value, and [value type](https://shopify.dev/docs/apps/build/metafields/list-of-data-types). ### Parameters * **change** **MetafieldChange** **required** ### Returns * **Promise\** ### MetafieldChange A metafield change operation that can either update or remove a metafield. Pass this to \`applyMetafieldChange\` to modify discount settings stored in metafields. ```ts MetafieldUpdateChange | MetafieldRemoveChange ``` ### MetafieldUpdateChange A metafield update or creation operation. Use this to set or modify metafield values that store discount function configuration data. * key The unique key identifying the metafield within its namespace. Use descriptive keys that indicate the setting's purpose (for example, \`'min\_purchase\_amount'\` or \`'eligible\_customer\_tags'\`). ```ts string ``` * namespace The namespace that organizes related metafields. When omitted, a default namespace is assigned. Use consistent namespaces to group related settings. ```ts string ``` * type Identifies this as an update operation. Always set to \`'updateMetafield'\` for updates. ```ts 'updateMetafield' ``` * value The metafield value to store. Can be a string or number depending on your configuration needs. ```ts string | number ``` * valueType The \[data type]\(/docs/apps/build/metafields/list-of-data-types) that defines how the value is formatted and validated. When omitted, preserves the existing type for updates or uses a default for new metafields. Choose a type that matches your value format. ```ts SupportedDefinitionType ``` ### SupportedDefinitionType The supported \[metafield definition types]\(/docs/apps/build/metafields/list-of-data-types) for storing extension configuration data. Use these types to specify how metafield values should be formatted, validated, and displayed. Types prefixed with \`list.\` store arrays of values, while other types store single values. Choose a type that matches your data format (for example, use \`'number\_integer'\` for whole numbers, \`'single\_line\_text\_field'\` for short text, or \`'json'\` for complex structured data). ```ts 'boolean' | 'collection_reference' | 'color' | 'date' | 'date_time' | 'dimension' | 'file_reference' | 'json' | 'metaobject_reference' | 'mixed_reference' | 'money' | 'multi_line_text_field' | 'number_decimal' | 'number_integer' | 'page_reference' | 'product_reference' | 'rating' | 'rich_text_field' | 'single_line_text_field' | 'product_taxonomy_value_reference' | 'url' | 'variant_reference' | 'volume' | 'weight' | 'list.collection_reference' | 'list.color' | 'list.date' | 'list.date_time' | 'list.dimension' | 'list.file_reference' | 'list.metaobject_reference' | 'list.mixed_reference' | 'list.number_decimal' | 'list.number_integer' | 'list.page_reference' | 'list.product_reference' | 'list.rating' | 'list.single_line_text_field' | 'list.url' | 'list.variant_reference' | 'list.volume' | 'list.weight' ``` ### MetafieldRemoveChange A metafield removal operation. Use this to delete metafields that are no longer needed for your discount configuration. * key The unique key of the metafield to remove. Must match the key used when the metafield was created. ```ts string ``` * namespace The namespace containing the metafield to remove. Required to ensure the correct metafield is targeted, as the same key can exist in different namespaces. ```ts string ``` * type Identifies this as a removal operation. Always set to \`'removeMetafield'\` for deletions. ```ts 'removeMetafield' ``` ### MetafieldChangeResult The result returned after attempting to change a metafield. Check the \`type\` property to determine if the operation succeeded (\`'success'\`) or failed (\`'error'\`), then handle the result appropriately in your extension. ```ts MetafieldChangeSuccess | MetafieldChangeResultError ``` ### MetafieldChangeSuccess A successful metafield change operation result. The metafield was updated or removed as requested and the changes are now saved. * type Indicates the operation succeeded. When this value is \`'success'\`, the metafield change was applied successfully. ```ts 'success' ``` ### MetafieldChangeResultError A failed metafield change operation result. Use the error message to understand what went wrong and fix the issue, such as validation errors, permission problems, or invalid metafield types. * message A human-readable error message explaining why the operation failed. Use this to debug issues or display feedback to merchants. ```ts string ``` * type Indicates the operation failed. Check this value to determine if you need to handle an error. ```ts 'error' ``` ### data The `data` object exposed to the extension containing the discount function settings. Provides access to the discount identifier and associated [metafields](https://shopify.dev/docs/apps/build/metafields) that store function configuration values. Use this data to populate your settings UI and understand the current function configuration in the `admin.discount-details.function-settings.render` target. * **id** **string** **required** The discount's unique global identifier (GID) in the [GraphQL Admin API](https://shopify.dev/docs/api/admin-graphql) format (for example, `gid://shopify/DiscountAutomaticApp/123`). Use this ID to associate settings with the correct discount or query discount data. * **metafields** **Metafield\[]** **required** An array of [metafields](https://shopify.dev/docs/apps/build/metafields) that store the discount function's configuration values. Use these metafields to populate your settings UI with the current discount configuration and display existing settings to merchants. ### Metafield A \[metafield]\(/docs/apps/build/metafields) that stores discount function configuration data. Use metafields to persist settings that control how your discount function behaves, such as discount thresholds, eligibility rules, or custom discount logic parameters. * description A human-readable description explaining the metafield's purpose and how it affects discount behavior. Use this to document your settings for other developers. ```ts string ``` * id The unique global identifier (GID) for this metafield. Use this ID to reference the metafield in GraphQL queries or updates. ```ts string ``` * key The unique key identifying this metafield within its namespace. This key determines how you access the metafield value (for example, \`'min\_purchase\_amount'\` or \`'eligible\_customer\_tags'\`). ```ts string ``` * namespace The namespace that organizes related metafields together. All metafields for a discount should use a consistent namespace to group related settings. ```ts string ``` * type The metafield \[definition type]\(/docs/apps/build/metafields/list-of-data-types) that specifies the value format and validation rules. Use this to determine how to parse and display the value. ```ts string ``` * value The metafield value stored as a string. Parse this value according to the metafield type to use it in your settings UI. ```ts string ``` Examples ### Examples * #### ##### Description Save a minimum purchase threshold to a metafield with decimal number validation. This example uses a \[text field]\(/docs/api/admin-extensions/2026-01/web-components/forms/text-field) for input, calls \`applyMetafieldChange\`, and displays success or error feedback. ##### jsx ```tsx import {render} from 'preact'; import {useState} from 'preact/hooks'; export default async () => { render(, document.body); }; function Extension() { const [threshold, setThreshold] = useState('50.00'); const [saved, setSaved] = useState(false); const handleSave = async () => { const result = await shopify.applyMetafieldChange({ type: 'updateMetafield', namespace: 'discount-config', key: 'minimum_purchase', value: threshold, valueType: 'number_decimal', }); if (result.type === 'success') { setSaved(true); } else { console.error('Configuration failed:', result.message); } }; return ( setThreshold(value)} /> Save Threshold {saved && Threshold configured!} ); } ``` * #### ##### Description Save multiple discount configuration settings in a single operation. This example stores customer tags as JSON and usage limits as an integer, demonstrating how to apply multiple metafield changes sequentially. ##### jsx ```tsx import {render} from 'preact'; import {useState} from 'preact/hooks'; export default async () => { render(, document.body); }; function Extension() { const [tags, setTags] = useState('vip, wholesale, premium'); const [maxUses, setMaxUses] = useState('5'); const handleSave = async () => { await shopify.applyMetafieldChange({ type: 'updateMetafield', namespace: 'discount-config', key: 'eligible_customer_tags', value: JSON.stringify(tags.split(',').map((t) => t.trim())), valueType: 'json', }); await shopify.applyMetafieldChange({ type: 'updateMetafield', namespace: 'discount-config', key: 'max_uses_per_customer', value: maxUses, valueType: 'number_integer', }); }; return ( setTags(value)} /> setMaxUses(value)} /> Save Eligibility Rules ); } ``` * #### ##### Description Load discount metafields on mount and display current configuration. This example shows reducing metafields into a settings object, checking for missing values, and applying defaults only when needed. ##### jsx ```tsx import {render} from 'preact'; import {useState, useEffect} from 'preact/hooks'; export default async () => { render(, document.body); }; function Extension() { const {data} = shopify; const [settings, setSettings] = useState({}); useEffect(() => { const initializeSettings = async () => { const existingSettings = data.metafields.reduce((acc, field) => { acc[field.key] = field.value; return acc; }, {}); setSettings(existingSettings); if (!existingSettings.eligible_tags) { await shopify.applyMetafieldChange({ type: 'updateMetafield', namespace: 'discount-config', key: 'eligible_tags', value: JSON.stringify(['vip', 'wholesale']), valueType: 'json', }); } }; initializeSettings(); }, [data]); return ( Current settings: {Object.entries(settings).map(([key, value]) => ( {key}: {String(value)} ))} ); } ``` *** ## Best practices * **Check operation result type:** `applyMetafieldChange` returns `{ type: 'success' }` or `{ type: 'error', message: string }`. Errors don't throw exceptions, so always check the returned `type` property. *** ## Limitations * Metafields are subject to [size limits](https://shopify.dev/docs/apps/build/metafields/metafield-limits). Individual metafield values can't exceed 256KB, and total metafields per resource have storage limits. * The `applyMetafieldChange` method is sequential. Operations are processed one at a time. Rapid successive calls may lead to race conditions where later updates overwrite earlier ones. * Metafield changes are applied immediately. Unlike some admin forms, metafield changes persist right away without waiting for the merchant to save the discount. ***