Where can I find the functionId for my Shopify app's discount functions when using the discountAutom

app.post(“/api/create-discount”, async (req, res) => {
try {
const session = res.locals.shopify.session;
const discountData = await createDiscount(session, req.body);

if (discountData.error) {
return res.status(400).json(discountData.error);
}

res.json(discountData);
} catch (error) {
console.error(“Discount creation error:”, error);
res.status(500).json({ error: “Failed to create discount” });
}
});

const createDiscount = async (session, body) => {
try {
const client = new shopify.api.clients.Graphql({ session });

const response = await client.query({
data: {
query: mutation discountAutomaticAppCreate($automaticAppDiscount: DiscountAutomaticAppInput!) { discountAutomaticAppCreate(automaticAppDiscount: $automaticAppDiscount) { userErrors { field message } automaticAppDiscount { discountId title startsAt endsAt status appDiscountType { appKey functionId } combinesWith { orderDiscounts productDiscounts shippingDiscounts } } } },
variables: {
automaticAppDiscount: {
title: “$5 discount”,
functionId: “?”, // how to find this or create
startsAt: “2025-02-02T17:09:21Z”,
endsAt: “2025-02-02T17:09:21Z”,
combinesWith: {
orderDiscounts: false,
productDiscounts: false,
shippingDiscounts: false,
},
metafields: [
{
namespace: “default”,
key: “function-configuration”,
type: “json”,
value: JSON.stringify({
discounts: [
{
value: { fixedAmount: { amount: 5 } },
targets: [
{
orderSubtotal: {
excludedVariantIds: ,
},
},
],
},
],
discountApplicationStrategy: “FIRST”,
}),
},
],
},
},
},
});

const result = response.body.data.discountAutomaticAppCreate;

if (result.userErrors.length > 0) {
throw new Error(result.userErrors.map((e) => e.message).join(", "));
}

return { success: true, data: result.automaticAppDiscount };
} catch (error) {
console.error(“Discount creation failed:”, error);
return { error: error.message };
}
};

Great question! The functionId you’re asking about is essential for Shopify Functions-based discounts, and it’s not generated automatically—you need to register your Function in your app configuration, then use the generated ID when creating the discount.

Here’s how you can find or generate your functionId:


:magnifying_glass_tilted_left: Where to Get the functionId#### 1. After Deploying Your Shopify Function

If you’re using the Shopify CLI to build and deploy a function (e.g., using shopify app dev), after you run:

shopify app deploy

or

shopify app function deploy

Shopify will register the function and output a functionId in the console. You can copy it from there.


2. Via Shopify Admin API

If you need to retrieve the functionId programmatically, you can use this query to fetch all available function IDs for your app:

{
  app {
    functions(first: 10) {
      edges {
        node {
          id
          title
          apiType
        }
      }
    }
  }
}

In your backend (Node.js + GraphQL client):

const response = await client.query({
  data: {
    query: `
      {
        app {
          functions(first: 10) {
            edges {
              node {
                id
                title
                apiType
              }
            }
          }
        }
      }
    `,
  },
});

Use the id from the result as your functionId.


:wrench: Example Output:

{
  "data": {
    "app": {
      "functions": {
        "edges": [
          {
            "node": {
              "id": "gid://shopify/AppFunction/1234567890",
              "title": "5 Dollar Off",
              "apiType": "DISCOUNT"
            }
          }
        ]
      }
    }
  }
}

Use this value for functionId:

functionId: "gid://shopify/AppFunction/1234567890"

? Pro Tips:- Make sure your function is deployed and active in your app before using it in a discountAutomaticAppCreate.

  • The functionId corresponds to the exact logic your Shopify Function defines. If you update the function logic, you may want to re-deploy and verify it’s still the same.

Let me know if you want help creating or deploying a Shopify Function (like a discount function) from scratch—I can walk you through that too.

Here is how I did find the function Id, I did not find it after the deploy command or the above graphql did not work for me!
After running the app press g it opens up GraphiQL (Admin API) in the browser then run the below

query GetFunctions {
    shopifyFunctions(first: 10) {
      nodes {
        id
        title
      }
    }
  }

[/quote]