Create a discount code through Python API

New Member
3 0 0


I'm using shopify_python_api to create a discount code to $5 off $85 orders. What am I doing wrong?


price_rule = shopify.PriceRule.create({
    "price_rule_id" : 11111,
    "title" : "5_OFF_85_000001",
    "target_type" : "line_item",
    "target_selection" : "entitled",
    "allocation_method" : "across",
    "value_type" : "fixed_amount",
    "value" : "-5",
    "once_per_customer" : True,
    "prerequisite_subtotal_range" : {"greater_than_or_equal_to" : "85.0"},
    "customer_selection" : "all",
    "starts_at" :"%Y-%m-%d %H:%M:%S"),
    "ends_at" : ( + timedelta(days=5)).strftime("%Y-%m-%d %H:%M:%S")

discount_code = shopify.DiscountCode({
    "code" : "5_OFF_85-AAA111",
    "id" : int(time.time())

discount_code_res = price_rule.add_discount_code(discount_code)


I get the following error. I tried changing 11111 to "11111", but it made no difference.


Traceback (most recent call last):
  File "/home/gerald/shopify/lib/python3.9/site-packages/pyactiveresource/", line 286, in _open
    http_response = self._handle_error(self._urlopen(request))
  File "/home/gerald/shopify/lib/python3.9/site-packages/pyactiveresource/", line 316, in _urlopen
    return urllib.request.urlopen(request, timeout=self.timeout)
  File "/usr/lib64/python3.9/urllib/", line 214, in urlopen
    return, data, timeout)
  File "/usr/lib64/python3.9/urllib/", line 523, in open
    response = meth(req, response)
  File "/usr/lib64/python3.9/urllib/", line 632, in http_response
    response = self.parent.error(
  File "/usr/lib64/python3.9/urllib/", line 561, in error
    return self._call_chain(*args)
  File "/usr/lib64/python3.9/urllib/", line 494, in _call_chain
    result = func(*args)
  File "/usr/lib64/python3.9/urllib/", line 641, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 400: Bad Request

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/gerald/shopify/lib/python3.9/site-packages/shopify/resources/", line 9, in add_discount_code
    resource ="discount_codes", discount_code.encode())
  File "/home/gerald/shopify/lib/python3.9/site-packages/pyactiveresource/", line 1117, in _instance_post
    return, self.klass.headers, body)
  File "/home/gerald/shopify/lib/python3.9/site-packages/pyactiveresource/", line 375, in post
    return self._open('POST', path, headers=headers, data=data)
  File "/home/gerald/shopify/lib/python3.9/site-packages/shopify/", line 26, in _open
    self.response = super(ShopifyConnection, self)._open(*args, **kwargs)
  File "/home/gerald/shopify/lib/python3.9/site-packages/pyactiveresource/", line 288, in _open
    http_response = self._handle_error(err)
  File "/home/gerald/shopify/lib/python3.9/site-packages/pyactiveresource/", line 413, in _handle_error
    raise BadRequest(err)
pyactiveresource.connection.BadRequest: Response(code=400, body="b'{"errors":{"price_rule_id":"expected String to be a id"}}'", headers={'Date': 'Fri, 26 Mar 2021 00:16:24 GMT', ..., msg="Bad Request")


I tried enabling debug mode to see the requests, but I can't find the issue.


2021-03-25 20:01:52,202 pyactiveresource.connection DEBUG:request-body:b'{"price_rule": {"price_rule_id": 11111, "title": "5_OFF_85_000001", "target_type": "line_item", "target_selection": "entitled", "allocation_method": "across", "value_type": "fixed_amount", "value": "-5", "once_per_customer": true, "prerequisite_subtotal_range": {"greater_than_or_equal_to": "85.0"}, "customer_selection": "all", "starts_at": "2021-03-25 20:01:52", "ends_at": "2021-03-30 20:01:52"}}'

2021-03-25 20:01:52,458 pyactiveresource.connection DEBUG:request-headers:User-agent:ShopifyPythonAPI/8.3.0 Python/3.9.2
2021-03-25 20:01:52,458 pyactiveresource.connection DEBUG:request-body:b'{"discount_code": {"code": "GL_5_OFF_85-5OZHR3E6BR", "id": 1616716912}}'




Shopify Staff
Shopify Staff
92 20 15

Hi Gerald,

From the PriceRule resource doc, I don't think you need to specify a price_rule_id when creating a price rule. Also, since you're creating this price rule as an entitlement, I believe you would have to provide the resources (using an entitled_ property) for which this price rule would apply.

If you're still not successful. Could you provide the x-request-id (found in the response header) of the query that fails and I could check from the logs what could be going wrong. Also if the issue is peculiar to the python API, then please open an issue here.


syf_ | Developer Support @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit or the Shopify Web Design and Development Blog