Create Magento Coupon via API

Create Magento Coupon via API

August 20, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Create Magento Coupon via API

Creating Magento Coupon Codes via API: A Comprehensive Guide

In the dynamic world of e-commerce, generating coupon codes programmatically is not only efficient but also crucial for your business’s success. If you’re looking to create new coupon codes (Shopping Cart Price Rules) in Magento Community Edition through the API, this guide will walk you through a step-by-step process.

Understanding the Magento API

Magento provides SOAP and REST APIs that allow external applications to interact with its core functionalities. While the default API may not directly support creating coupons, we can achieve this by leveraging the existing resources.

Creating a New Coupon via API

To create a new coupon code programmatically, you’ll need to create a Shopping Cart Price Rule first and then associate it with a specific product or customer group. Here’s how:

  1. Create a Shopping Cart Price Rule (Coupon)

First, you can use the SOAP v2 API to create a new shopping cart price rule with the following parameters:

  • rule_name: The coupon code name.
  • simple_action: The type of discount (e.g., “fixed”, “percent”).
  • disc_amount: The amount or percentage value for the discount.
  • apply_to: The products or customer groups that this coupon will apply to.

Here’s a sample PHP script using the Magento SoapClient:

$client = new SoapClient('http://magentohost.com/api/soap/?wsdl');
$sessionId = $client->login('apiUser', 'apiKey');

$result = $client->call(
    array(
        'index' => 'catalog_product_price',
        'interfaceName' => 'catalog_product_priceV1',
        'operation' => 'createRule',
        'parameters' => array(
            array(
                'rule_name' => 'CouponCode123',
                'simple_action' => 'fixed',
                'disc_amount' => 50,
                'apply_to' => '%'
            )
        )
    ),
    $sessionId
);

Remember to replace 'apiUser' and 'apiKey' with your Magento API credentials. The apply_to parameter can be a product ID % for all products.

  1. Associating the Coupon with Products or Customer Groups

To make sure the coupon applies to specific products, you’ll need to associate it with those items using the catalog_product_priceV1->assignRule method:

$client = new SoapClient('http://magentohost.com/api/soap/?wsdl');
$sessionId = $client->login('apiUser', 'apiKey');

$result = $client->call(
    array(
        'index' => 'catalog_product_price',
        'interfaceName' => 'catalog_product_priceV1',
        'operation' => 'assignRule',
        'parameters' => array(
            array(
                'rule_id' => 20, // The ID of the coupon you just created
                'product' => '%' // To apply to all products
            )
        )
    ),
    $sessionId
);

Conclusion

Creating Magento coupon codes via the API requires a strategic approach that involves creating shopping cart price rules and associating them with specific products or customer groups. By following this guide, you can now generate coupons programmatically for your Magento store.

Remember to always use secure communication channels when working with APIs, especially those involving sensitive data like coupon amounts. For more advanced use cases, consider exploring REST API integration or leveraging Magento’s event system for custom coupon generation logic.

At Laravel Company, we specialize in helping businesses harness the power of Magento and its APIs to drive e-commerce growth. Contact us today to learn how we can help you optimize your coupon management strategy.

Happy coding!

Checkout Catalog API Customers

Our network