Discounts in depth

This section provides a deeper level of immersion into specific concepts and tasks that are important in understanding how you can leverage Recharge discounts in your store.

Discount codes can be used in a variety of ways, from a single purchase to a recurring subscription.


Creating discount codes

When you want to create a discount in the merchant portal, there are several filters you can use to refine and limit its application. See Creating discount codes for detailed instructions.

The following table displays the direct mappings between Merchant Portal UI settings and the parameters you can pass in an API request using Recharge API v. 2021-11.

NumberMerchant portalRecharge APIDescription
1Type of discountvalue_typeThe type of discount.
2Discount codecodeThe name of the discount.
3Applies toapplies_to:
resource
ids
purchase_item_type
The rules that specify how the discount can be applied.
4Channel Controlschannel_settingsThe channels (Merchant Portal, Customer Portal, API) where the discount can be applied.
5Application limitsusage_limits:
first_time_customer_restriction
one_application _per_customer
The rules that designate the number of times a discount can be applied and whether discounts can only be applied for new customers.
6Date Rangestarts_at
ends_at
The date range for when the discount can be applied.


Applying a discount automatically via URL

🚧

Note:

Applying an automatic discount via URL requires your store to be on Recharge checkout on Shopify, as automatic discounts are not supported on the Shopify Checkout Integration or Recharge Checkout on BigCommerce.

To add an automatic discount code, pass the discount code in the URL.

Example URL request

/r/checkout?myshopify_domain=bootstrap-dev.myshopify.com&cart_token={your_token}&discount={discount_name}

For more information on automatically applying a discount via URL, refer to Apply discount code automatically at checkout.


Applying a discount automatically via Recharge API

The checkout endpoint is only available for BigCommerce and Custom integrations. If you want to perform checkouts on Shopify, you must go through Shopify. The checkout object is currently only available for Recharge API version 2021-01. This feature will be available soon for Recharge API version 2021-11.

To apply a discount automatically using the Recharge API, you may perform the following actions:

  • Create a new checkout
  • Update a checkout

Create a new checkout

Before creating a checkout, the discount you want to apply must first exist in Recharge. The first step in automatically applying a discount code to a checkout via the Recharge API is to create the checkout. You can apply a discount in the checkout, update, or process steps.

The following example shows how you can create a checkout.

curl --location --request POST 'https://api.rechargeapps.com/checkouts' \
--header 'X-Recharge-Access-Token: {your_access_token} \
--header 'Content-Type: application/json' \
--data-raw '{
 "analytics_data": {
   "utm_params": [{
     "utm_campaign": "spring_sale",
     "utm_content": "textlink",
     "utm_data_source": "website_cookie",
     "utm_medium": "cpc",
     "utm_source": "google",
     "utm_term": "mleko",
     "utm_timestamp": "2020-03-05"
   }]
 },
 "discount_code": "POPUS_25",
 "email":"[email protected]",
 "external_checkout_id": "<cart_token>",
 "external_checkout_source": "headless",
 "line_items": [
   {
     "charge_interval_frequency": 5,
     "fulfillment_service": "manual",
     "order_interval_frequency": 5,
     "order_interval_unit": "day",
     "product_id": 3,
     "quantity": 6,
     "requires_shipping": "True",
     "taxable": "True",
     "variant_id": 3
   }
 ],
 "shipping_address": {
   "address1": "6419 Ocean Front Walk",
   "address2": "Apt 2",
   "city": "Los Angeles",
   "company": "",
   "country": "United States",
   "first_name": "Novak",
   "last_name": "Djokovic",
   "phone": "1-800-800-8000",
   "province": "California",
   "zip": "90293"
 }
}'

//Parameters explained:
//discount_code - the discount code you want applied to the checkout
//charge_interval_frequency - how often the customer will be charged
//order_interval_frequency -  how often the order will be fulfilled
//product_id - the ID of the product
//variant_id - the ID of the variant for the product
import requests
import json

url = "https://api.rechargeapps.com/checkouts"

payload = json.dumps({
 "analytics_data": {
   "utm_params": [
     {
       "utm_campaign": "spring_sale",
       "utm_content": "textlink",
       "utm_data_source": "website_cookie",
       "utm_medium": "cpc",
       "utm_source": "google",
       "utm_term": "mleko",
       "utm_timestamp": "2020-03-05"
     }
   ]
 },
 "discount_code": "POPUS_25",
 "email": "[email protected]",
 "external_checkout_id": "<cart_token>",
 "external_checkout_source": "headless",
 "line_items": [
   {
     "charge_interval_frequency": 5,
     "fulfillment_service": "manual",
     "order_interval_frequency": 5,
     "order_interval_unit": "day",
     "product_id": 3,
     "quantity": 6,
     "requires_shipping": "True",
     "taxable": "True",
     "variant_id": 3
   }
 ],
 "shipping_address": {
   "address1": "6419 Ocean Front Walk",
   "address2": "Apt 2",
   "city": "Los Angeles",
   "company": "",
   "country": "United States",
   "first_name": "Novak",
   "last_name": "Djokovic",
   "phone": "1-800-800-8000",
   "province": "California",
   "zip": "90293"
 }
})
headers = {
 'X-Recharge-Access-Token': {your_access_token},
 'Content-Type': 'application/json',
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)
require "json"
require "net/http"

url = URI("https://api.rechargeapps.com/checkouts")

https = Net::HTTP.new(url.host, url.port)
https.use_ssl = true

request = Net::HTTP::Post.new(url)
request["X-Recharge-Access-Token"] = "{your_access_token}"
request["Content-Type"] = "application/json"
request.body = JSON.dump({
  "analytics_data": {
    "utm_params": [{
      "utm_campaign": "spring_sale",
      "utm_content": "textlink",
      "utm_data_source": "website_cookie",
      "utm_medium": "cpc",
      "utm_source": "google",
      "utm_term": "mleko",
      "utm_timestamp": "2020-03-05"
    }]
  },
  "discount_code": "POPUS_25",
  "email": "[email protected]",
  "external_checkout_id": "<cart_token>",
  "external_checkout_source": "headless",
  "line_items": [{
    "charge_interval_frequency": 5,
    "fulfillment_service": "manual",
    "order_interval_frequency": 5,
    "order_interval_unit": "day",
    "product_id": 3,
    "quantity": 6,
    "requires_shipping": "True",
    "taxable": "True",
    "variant_id": 3
  }],
  "shipping_address": {
    "address1": "6419 Ocean Front Walk",
    "address2": "Apt 2",
    "city": "Los Angeles",
    "company": "",
    "country": "United States",
    "first_name": "Novak",
    "last_name": "Djokovic",
    "phone": "1-800-800-8000",
    "province": "California",
    "zip": "90293"
  }
})

response = https.request(request)
puts response.read_body
require_once 'HTTP/Request2.php';
$request = new HTTP_Request2();
$request->setUrl('https://api.rechargeapps.com/checkouts');
$request->setMethod(HTTP_Request2::METHOD_POST);
$request->setConfig(array(
 'follow_redirects' => TRUE
));
$request->setHeader(array(
 'X-Recharge-Access-Token' => '{your_access_token}',
));
$request->setBody('{\n  "analytics_data": {\n    "utm_params": [{\n      "utm_campaign": "spring_sale",\n      "utm_content": "textlink",\n      "utm_data_source": "website_cookie",\n      "utm_medium": "cpc",\n      "utm_source": "google",\n      "utm_term": "mleko",\n      "utm_timestamp": "2020-03-05"\n    }]\n  },\n  "discount_code": "POPUS_25",\n  "email":"[email protected]",\n  "external_checkout_id": "<cart_token>",\n  "external_checkout_source": "headless",\n  "line_items": [\n    {\n      "charge_interval_frequency": 5,\n      "fulfillment_service": "manual",\n      "order_interval_frequency": 5,\n      "order_interval_unit": "day",\n      "product_id": 3,\n      "quantity": 6,\n      "requires_shipping": "True",\n      "taxable": "True",\n      "variant_id": 3\n    }\n  ],\n  "shipping_address": {\n    "address1": "6419 Ocean Front Walk",\n    "address2": "Apt 2",\n    "city": "Los Angeles",\n    "company": "",\n    "country": "United States",\n    "first_name": "Novak",\n    "last_name": "Djokovic",\n    "phone": "1-800-800-8000",\n    "province": "California",\n    "zip": "90293"\n  }\n}');
try {
 $response = $request->send();
 if ($response->getStatus() == 200) {
   echo $response->getBody();
 }
 else {
   echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
   $response->getReasonPhrase();
 }
}
catch(HTTP_Request2_Exception $e) {
 echo 'Error: ' . $e->getMessage();
}

Update the checkout with a discount code

When the checkout has been created, you may update the checkout with a discount code so it can be applied to the checkout before processing.

The following example shows how you can update a checkout.

curl --location --request PUT 'https://api.rechargeapps.com/checkouts/f34ba13ff3d54b55944d135e0864aaf5' \
--header 'X-Recharge-Access-Token: {your_access_token} \
--header 'Content-Type: application/json' \\
--data-raw '{
   "shipping_address": {
       "address1": "6419 Ocean Front Walk",
       "address2": "",
       "city": "Apt 2",
       "province": "California",
       "first_name": "Novak",
       "last_name": "Djokovic",
       "zip": "90293",
       "company": "Tennis Serbia",
       "phone": "1-800-800-8000",
       "country": "United States",
       "original_shipping_lines": [
           {
               "price": "43.20",
               "code": "Standard Shipping",
               "title": "Standard Shipping"
           }
       ],
       "shipping_lines_override": null,
       "discount_code": "FEB22DISCOUNT"
   }
}'

//Parameters explained
//discount_code - the discount code being applied to the checkout
import requests
import json

url = "https://api.rechargeapps.com/checkouts/f34ba13ff3d54b55944d135e0864aaf5"

payload = json.dumps({
 "shipping_address": {
   "address1": "6419 Ocean Front Walk",
   "address2": "",
   "city": "Apt 2",
   "province": "California",
   "first_name": "Novak",
   "last_name": "Djokovic",
   "zip": "90293",
   "company": "Tennis Serbia",
   "phone": "1-800-800-8000",
   "country": "United States",
   "original_shipping_lines": [
     {
       "price": "43.20",
       "code": "Standard Shipping",
       "title": "Standard Shipping"
     }
   ],
   "shipping_lines_override": None,
   "discount_code": "FEB22DISCOUNT"
 }
})
headers = {
 'X-Recharge-Access-Token': {your_access_token},
 'Content-Type': 'application/json',
}

response = requests.request("PUT", url, headers=headers, data=payload)

print(response.text)
require "uri"
require "json"
require "net/http"

url = URI("https://api.rechargeapps.com/checkouts/0e51712b541f4d168486fb87ce8eb87d")

https = Net::HTTP.new(url.host, url.port)
https.use_ssl = true

request = Net::HTTP::Put.new(url)
request["X-Recharge-Access-Token"] = "{your_access_token}"
request["Content-Type"] = "application/json"
request.body = JSON.dump({
  "shipping_address": {
    "address1": "6419 Ocean Front Walk",
    "address2": "",
    "city": "Apt 2",
    "province": "California",
    "first_name": "Novak",
    "last_name": "Djokovic",
    "zip": "90293",
    "company": "Tennis Serbia",
    "phone": "1-800-800-8000",
    "country": "United States",
    "original_shipping_lines": [{
      "price": "43.20",
      "code": "Standard Shipping",
      "title": "Standard Shipping"
    }],
    "shipping_lines_override": nil,
    "discount_code": "FEB22DISCOUNT"
  }
})

response = https.request(request)
puts response.read_body
require_once 'HTTP/Request2.php';
$request = new HTTP_Request2();
$request->setUrl('https://api.rechargeapps.com/checkouts/0e51712b541f4d168486fb87ce8eb87d');
$request->setMethod(HTTP_Request2::METHOD_PUT);
$request->setConfig(array(
 'follow_redirects' => TRUE
));
$request->setHeader(array(
 'X-Recharge-Access-Token' => '{your_access_token}',
 'Content-Type' => 'application/json',
));
$request->setBody('{\n    "shipping_address": {\n       "address1": "6419 Ocean Front Walk",\n      "address2": "",\n       "city": "Apt 2",\n      "province": "California",\n     "first_name": "Novak",\n        "last_name": "Djokovic",\n      "zip": "90293",\n       "company": "Tennis Serbia",\n       "phone": "1-800-800-8000",\n        "country": "United States",\n       "original_shipping_lines": [\n          {\n             "price": "43.20",\n             "code": "Standard Shipping",\n              "title": "Standard Shipping"\n          }\n     ],\n        "shipping_lines_override": null,\n        "discount_code": "FEB22DISCOUNT"\n    }\n}');
try {
 $response = $request->send();
 if ($response->getStatus() == 200) {
   echo $response->getBody();
 }
 else {
   echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
   $response->getReasonPhrase();
 }
}
catch(HTTP_Request2_Exception $e) {
 echo 'Error: ' . $e->getMessage();
}

Detailed discount flow

Discounts can be applied in several different ways, depending on whether you want to apply a discount for a one-time charge, or recurring charge.

There are 3 validations that will happen during the life of the discount:

  • At creation, we check that all the 'Required' params were specified before creating the discount.
  • At application, we check that the rules defined for the checkout are being respected by the checkout or charge this discount is supposed to be applied to.
  • At recurring charges,for every recurring charge processed we check: does the discount still apply?

The following flow chart illustrates the step-by-step process of how a discount can be applied for these two workflows.

For more information about discount modeling, refer to Discounts overview .


Async batches for discount creation

🚧

Note:

Async batches are only available in Recharge API version 2021-01. This feature will be coming soon for API version 2021-11.

The Async batches resource can be used for processing large volumes of operations asynchronously, in order to reduce aggregate processing time and network traffic when interacting with many unique objects. For example, a user can leverage this resource to create 1000 discounts with only 3 API requests. Using async batches for discount code creation requires you to perform several steps to accomplish this task.

These steps include:

  • Creating a batch
  • Adding a task list to a batch
  • Processing the batch
  • Retrieving the batch to verify discount creation

Creating a batch

To create a new batch, you need to make a POST API request to the async_batches endpoint. This request creates a new ID for the batch that you will need to use in subsequent batch operations.

curl --location --request POST 'https://api.rechargeapps.com/async_batches' \
--header 'X-Recharge-Access-Token: {your_access_token} \
--header 'Content-Type: application/json' \
--data-raw '{
   "batch_type": "discount_create"
}'

//Parameters explained
//batch_type - The type of batch created. In this example, discount_create.

import requests
import json

url = "https://api.rechargeapps.com/async_batches"

payload = json.dumps({
 "batch_type": "discount_create"
})
headers = {
 'X-Recharge-Access-Token': {your_access_token},
 'Content-Type': 'application/json',
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)
require "uri"
require "json"
require "net/http"

url = URI("https://api.rechargeapps.com/async_batches")

https = Net::HTTP.new(url.host, url.port)
https.use_ssl = true

request = Net::HTTP::Post.new(url)
request["X-Recharge-Access-Token"] = "{your_access_token}”
request["Content-Type"] = "application/json"
request.body = JSON.dump({
  "batch_type": "discount_create"
})

response = https.request(request)
puts response.read_body
require_once 'HTTP/Request2.php';
$request = new HTTP_Request2();
$request->setUrl('https://api.rechargeapps.com/async_batches');
$request->setMethod(HTTP_Request2::METHOD_POST);
$request->setConfig(array(
 'follow_redirects' => TRUE
));
$request->setHeader(array(
 'X-Recharge-Access-Token' => '{your_access_token}',
 'Content-Type' => 'application/json',
));
$request->setBody('{\n  "batch_type": "discount_create"\n}');
try {
 $response = $request->send();
 if ($response->getStatus() == 200) {
   echo $response->getBody();
 }
 else {
   echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
   $response->getReasonPhrase();
 }
}
catch(HTTP_Request2_Exception $e) {
 echo 'Error: ' . $e->getMessage();
}

After you have created the batch, make sure to copy down the batch_id value. You will need this batch_id to add tasks to a batch and process the batch. An example of the response with the batch_id is shown below.

Adding a task to a batch

Once you have created the batch, you need to then add a list of tasks you want performed in the batch, with each task representing a single API request. To add tasks to a batch, you need to make a POST request to the async_batches endpoint, with the list of tasks you want to have performed in the body of the request. Be sure to include the batch_id in your request.

🚧

Note:

The following example shows just some of the parameters you can pass in the request body. For a complete list of parameters that can be passed, refer to the Create Discounts endpoint in the Recharge API Reference Guide.

curl --location --request POST 'https://api.rechargeapps.com/async_batches/{batch_id}/tasks' \
--header 'X-Recharge-Access-Token: {your_access_token} \
--header 'Content-Type: application/json' \
--data-raw '{
   "tasks": [
   {
     "body": {
       "code": "indexing",
       "discount_type": "shipping",
       "duration": "forever",
       "status": "enabled",
       "value": 100
     }
   },
   {
     "body": {
       "code": "maroon",
       "discount_type": "shipping",
       "duration": "forever",
       "status": "enabled",
       "value": 100
     }
   },
   {
     "body": {
       "code": "web services",
       "value": 100,
       "discount_type": "shipping",
       "duration": "forever",
       "status": "enabled"
     }
   },
   {
     "body": {
       "code": "PCI",
       "discount_type": "shipping",
       "duration": "forever",
       "status": "enabled",
       "value": 100
     }
   },
   {
     "body": {
       "code": "Cheese",
       "discount_type": "shipping",
       "duration": "forever",
       "status": "enabled",
       "value": 100
     }
   },
   {
     "body": {
       "code": "overriding",
       "discount_type": "shipping",
       "duration": "forever",
       "status": "enabled",
       "value": 100
     }
   },
   {
     "body": {
       "code": "bandwidth-monitored",
       "value": 100,
       "discount_type": "shipping",
       "duration": "forever",
       "status": "enabled"
     }
   },
   {
     "body": {
       "code": "Falkland Islands (Malvinas)",
       "discount_type": "shipping",
       "duration": "forever",
       "status": "enabled",
       "value": 100
     }
   },
   {
     "body": {
       "code": "Computer",
       "discount_type": "shipping",
       "duration": "forever",
       "status": "enabled",
       "value": 100
     }
   },
   {
     "body": {
       "code": "digital",
       "discount_type": "shipping",
       "duration": "forever",
       "status": "enabled",
       "value": 100
     }
   }
 ]
}'

//Parameters explained
//code - the name of the discount
//discount_type - the type of discount
//duration - the length of time the discount can be used
//status - the current status of the discount
//value - value of the discount

import requests
import json

url = "https://api.rechargeapps.com/async_batches/{batch_id}/tasks"

payload = json.dumps({
 "tasks": [
   {
     "body": {
       "code": "indexing",
       "discount_type": "shipping",
       "duration": "forever",
       "status": "enabled",
       "value": 100
     }
   },
   {
     "body": {
       "code": "maroon",
       "discount_type": "shipping",
       "duration": "forever",
       "status": "enabled",
       "value": 100
     }
   },
   {
     "body": {
       "code": "web services",
       "value": 100,
       "discount_type": "shipping",
       "duration": "forever",
       "status": "enabled"
     }
   },
   {
     "body": {
       "code": "PCI",
       "discount_type": "shipping",
       "duration": "forever",
       "status": "enabled",
       "value": 100
     }
   },
   {
     "body": {
       "code": "Cheese",
       "discount_type": "shipping",
       "duration": "forever",
       "status": "enabled",
       "value": 100
     }
   },
   {
     "body": {
       "code": "overriding",
       "discount_type": "shipping",
       "duration": "forever",
       "status": "enabled",
       "value": 100
     }
   },
   {
     "body": {
       "code": "bandwidth-monitored",
       "value": 100,
       "discount_type": "shipping",
       "duration": "forever",
       "status": "enabled"
     }
   },
   {
     "body": {
       "code": "Falkland Islands (Malvinas)",
       "discount_type": "shipping",
       "duration": "forever",
       "status": "enabled",
       "value": 100
     }
   },
   {
     "body": {
       "code": "Computer",
       "discount_type": "shipping",
       "duration": "forever",
       "status": "enabled",
       "value": 100
     }
   },
   {
     "body": {
       "code": "digital",
       "discount_type": "shipping",
       "duration": "forever",
       "status": "enabled",
       "value": 100
     }
   }
 ]
})
headers = {
 'X-Recharge-Access-Token': {your_access_token},
 'Content-Type': 'application/json',
}
response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)
require "uri"
require "json"
require "net/http"

url = URI("https://api.rechargeapps.com/async_batches/763041/tasks")

https = Net::HTTP.new(url.host, url.port)
https.use_ssl = true

request = Net::HTTP::Post.new(url)
request["X-Recharge-Access-Token"] = "{your_access_token}"
request["Content-Type"] = "application/json"
request.body = JSON.dump({
  "tasks": [{
      "body": {
        "code": "indexing",
        "discount_type": "shipping",
        "duration": "forever",
        "status": "enabled",
        "value": 100
      }
    },
    {
      "body": {
        "code": "maroon",
        "discount_type": "shipping",
        "duration": "forever",
        "status": "enabled",
        "value": 100
      }
    },
    {
      "body": {
        "code": "web services",
        "value": 100,
        "discount_type": "shipping",
        "duration": "forever",
        "status": "enabled"
      }
    },
    {
      "body": {
        "code": "PCI",
        "discount_type": "shipping",
        "duration": "forever",
        "status": "enabled",
        "value": 100
      }
    },
    {
      "body": {
        "code": "Cheese",
        "discount_type": "shipping",
        "duration": "forever",
        "status": "enabled",
        "value": 100
      }
    },
    {
      "body": {
        "code": "overriding",
        "discount_type": "shipping",
        "duration": "forever",
        "status": "enabled",
        "value": 100
      }
    },
    {
      "body": {
        "code": "bandwidth-monitored",
        "value": 100,
        "discount_type": "shipping",
        "duration": "forever",
        "status": "enabled"
      }
    },
    {
      "body": {
        "code": "Falkland Islands (Malvinas)",
        "discount_type": "shipping",
        "duration": "forever",
        "status": "enabled",
        "value": 100
      }
    },
    {
      "body": {
        "code": "Computer",
        "discount_type": "shipping",
        "duration": "forever",
        "status": "enabled",
        "value": 100
      }
    },
    {
      "body": {
        "code": "digital",
        "discount_type": "shipping",
        "duration": "forever",
        "status": "enabled",
        "value": 100
      }
    }
  ]
})

response = https.request(request)
puts response.read_body
require_once 'HTTP/Request2.php';
$request = new HTTP_Request2();
$request->setUrl('https://api.rechargeapps.com/async_batches/763041/tasks');
$request->setMethod(HTTP_Request2::METHOD_POST);
$request->setConfig(array(
 'follow_redirects' => TRUE
));
$request->setHeader(array(
 'X-Recharge-Access-Token' => '{your_access_token}',
 'Content-Type' => 'application/json',
));
$request->setBody('{\n  "tasks": [\n    {\n      "body": {\n        "code": "indexing",\n        "discount_type": "shipping",\n        "duration": "forever",\n        "status": "enabled",\n        "value": 100\n      }\n    },\n    {\n      "body": {\n        "code": "maroon",\n        "discount_type": "shipping",\n        "duration": "forever",\n        "status": "enabled",\n        "value": 100\n      }\n    },\n    {\n      "body": {\n        "code": "web services",\n        "value": 100,\n        "discount_type": "shipping",\n        "duration": "forever",\n        "status": "enabled"\n      }\n    },\n    {\n      "body": {\n        "code": "PCI",\n        "discount_type": "shipping",\n        "duration": "forever",\n        "status": "enabled",\n        "value": 100\n      }\n    },\n    {\n      "body": {\n        "code": "Cheese",\n        "discount_type": "shipping",\n        "duration": "forever",\n        "status": "enabled",\n        "value": 100\n      }\n    },\n    {\n      "body": {\n        "code": "overriding",\n        "discount_type": "shipping",\n        "duration": "forever",\n        "status": "enabled",\n        "value": 100\n      }\n    },\n    {\n      "body": {\n        "code": "bandwidth-monitored",\n        "value": 100,\n        "discount_type": "shipping",\n        "duration": "forever",\n        "status": "enabled"\n      }\n    },\n    {\n      "body": {\n        "code": "Falkland Islands (Malvinas)",\n        "discount_type": "shipping",\n        "duration": "forever",\n        "status": "enabled",\n        "value": 100\n      }\n    },\n    {\n      "body": {\n        "code": "Computer",\n        "discount_type": "shipping",\n        "duration": "forever",\n        "status": "enabled",\n        "value": 100\n      }\n    },\n    {\n      "body": {\n        "code": "digital",\n        "discount_type": "shipping",\n        "duration": "forever",\n        "status": "enabled",\n        "value": 100\n      }\n    }\n  ]\n}');
try {
 $response = $request->send();
 if ($response->getStatus() == 200) {
   echo $response->getBody();
 }
 else {
   echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
   $response->getReasonPhrase();
 }
}
catch(HTTP_Request2_Exception $e) {
 echo 'Error: ' . $e->getMessage();
}

Processing the batch

The next step you need to take is to process the batch and have it perform the tasks you just added to it. This requires you to make a POST request to the async_batches endpoint like the following example.

curl --location --request POST 'https://api.rechargeapps.com/async_batches/{batch_id}/process' \
--header 'X-Recharge-Access-Token: {your_access_token} \
--header 'Content-Type: application/json' \
--data-raw '{}'
import requests
import json

url = "https://api.rechargeapps.com/async_batches/batch_id/process"

payload = json.dumps({})
headers = {
 'X-Recharge-Access-Token': {your_access_token},
 'Content-Type': 'application/json',
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)
require "uri"
require "json"
require "net/http"

url = URI("https://api.rechargeapps.com/async_batches/763041/process")

https = Net::HTTP.new(url.host, url.port)
https.use_ssl = true

request = Net::HTTP::Post.new(url)
request["X-Recharge-Access-Token"] = "{your_access_token}"
request["Content-Type"] = "application/json"
request.body = JSON.dump({})

response = https.request(request)
puts response.read_body
require_once 'HTTP/Request2.php';
$request = new HTTP_Request2();
$request->setUrl('https://api.rechargeapps.com/async_batches/763041/process');
$request->setMethod(HTTP_Request2::METHOD_POST);
$request->setConfig(array(
 'follow_redirects' => TRUE
));
$request->setHeader(array(
 'X-Recharge-Access-Token' => '{your_access_token}',
 'Content-Type' => 'application/json',
));
$request->setBody('{}');
try {
 $response = $request->send();
 if ($response->getStatus() == 200) {
   echo $response->getBody();
 }
 else {
   echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
   $response->getReasonPhrase();
 }
}
catch(HTTP_Request2_Exception $e) {
 echo 'Error: ' . $e->getMessage();
}

Retrieving the batch

The final step you want to take is to verify that the batch has been created. You can verify the discounts were created by making a GET API request to the async_batches endpoint with the batch_id, similar to the following example.

curl --location --request GET 'https://api.rechargeapps.com/async_batches/{batch_id} \
--header 'X-Recharge-Access-Token: {your_access_token} \
import requests

url = "https://api.rechargeapps.com/async_batches/{batch_id}"

payload={}
headers = {
 'X-Recharge-Access-Token': {your_access_token},
}

response = requests.request("GET", url, headers=headers, data=payload)

print(response.text)
require "uri"
require "net/http"

url = URI("https://api.rechargeapps.com/async_batches/763041")

https = Net::HTTP.new(url.host, url.port)
https.use_ssl = true

request = Net::HTTP::Get.new(url)
request["X-Recharge-Access-Token"] = "{your_access_token}”

response = https.request(request)
puts response.read_body
require_once 'HTTP/Request2.php';
$request = new HTTP_Request2();
$request->setUrl('https://api.rechargeapps.com/async_batches/763041');
$request->setMethod(HTTP_Request2::METHOD_GET);
$request->setConfig(array(
 'follow_redirects' => TRUE
));
$request->setHeader(array(
 'X-Recharge-Access-Token' => '{your_access_token}',
));
try {
 $response = $request->send();
 if ($response->getStatus() == 200) {
   echo $response->getBody();
 }
 else {
   echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
   $response->getReasonPhrase();
 }
}
catch(HTTP_Request2_Exception $e) {
 echo 'Error: ' . $e->getMessage();
}

Discount code sync with other platforms

The discount code sync feature is an efficient tool that enables you to bulk import discount codes from Shopify into Recharge. For more information on how discount code synchronization works, refer to Using the Shopify Discount Import.

This eliminates the manual process of having to create a CSV file and then contacting the Recharge Support team, or building out your own integration. When creating discount codes in bulk, you can click a few buttons in Recharge to import these discount codes into Recharge.

When using this feature, be aware of the following conditions:

  • This is a one-way import from Shopify into Recharge. This feature does not make any updates in Shopify.
  • There are certain discount codes that cannot be imported from Shopify. For a list of unsupported discount codes, refer to Using the Shopify Discount Import.
  • Discount codes cannot be kept in sync automatically because Shopify does not have webhooks available for discounts.
  • There is a limit of 100,000 discount codes for each import. If you need to import more than 100,000 discount codes in an import, refer to Managing discount codes in bulk.

Need Help? Contact Us