Activating all sales channels for products via API

Activating all sales channels for products via API

nekosys
Visitor
2 0 1

Hello,

I am in the process of creating products through the API, and I have encountered a challenge in activating all the sales channels for each product. When I use the admin panel, the process is straightforward: I simply open a product, select ‘Sales Channels,’ and check all the checkboxes.

However, I am uncertain about how to replicate this process using the API. I have done some research and found a discussion on this topic at Shopify Community. Unfortunately, it appears that the channels API endpoint mentioned in the discussion is now deprecated.

I was wondering if anyone in the community has experience or knowledge on how to accomplish this task using the current API endpoints? Your assistance and guidance would be greatly appreciated.

 

Thank you in advance for your help!

Replies 2 (2)

Usmanabbasi360
Shopify Partner
1 0 0

Did you find any solution?

CapucineMad
Visitor
3 0 0

Hello, I had a very similar problem. I want to open all sales channels of a product using the API. The documentation is not very clear to me. I finally found a solution, so I'm sharing it to help other developers.

async function shopifyGraphQL(query, variables) {
  const response = await fetch(`https://${shopName}.myshopify.com/admin/api/2025-01/graphql.json`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'X-Shopify-Access-Token': accessToken,
    },
    body: JSON.stringify({ query, variables }),
  });

  const responseBody = await response.json();
  if (responseBody.errors) {
    throw new Error('Erreur request GraphQL');
  }
}

async function publishProductToAllChannels() {
  try {
    const channelsQuery = `
      {
        channels(first: 100) {
          edges {
            node {
              id
              name
            }
          }
        }
      }
    `;

    const channelsData = await shopifyGraphQL(channelsQuery);
    const channels = channelsData.channels.edges.map(edge => edge.node);

    const publishMutation = `
      mutation publishProduct($productId: ID!, $channelId: ID!) {
        publishablePublish(id: $productId, input: { channelId: $channelId }) {
          userErrors {
            field
            message
          }
        }
      }
    `;

    for (const channel of channels) {
      const variables = {
        productId: idProduct, // like gid://shopify/Product/675256852
        channelId: channel.id,
      };

      await shopifyGraphQL(publishMutation, variables);
    }
  } catch (error) {
    console.error('Erreur:', error);
  }
}