# DiscountCode <div class="note"> <h4>Note</h4> <p>We recommend using the GraphQL Admin API to manage discounts. The <a href="/api/examples/discounts">Discount types</a> available in the GraphQL Admin API are intended to replace the GraphQL Admin <code>PriceRule</code> object and REST Admin <code>PriceRule</code> and <code>DiscountCode</code> resources.</p> </div> <p>You can use the PriceRule DiscountCode resource to create discount codes that enable specific discounts to be redeemed. Merchants can distribute discount codes to their customers using a variety of means, such as an email or URL, and customers can apply these codes at checkout.</p> <p>Each discount code belongs to a price rule, which holds the logic for the discount. For more information, see the <a href="/docs/admin-api/rest/reference/discounts/pricerule">PriceRule</a> resource.</p> <p>To create multiple discount codes that use the same price rule logic, use the batch endpoint. For example, you might allow merchants to create a batch of discount codes that belong to the same price rule but are each personalized for a different customer.</p> ## Resource Properties ### DiscountCode * code: <p>The case-insensitive discount code that customers use at checkout. (maximum: 255 characters)</p> <div class="note note-caution"> <p>Use the same value for <code>code</code> as the <code>title</code> property of the associated <a href="/docs/admin-api/rest/reference/discounts/pricerule/#title-property">price rule</a>.</p> </div> * Type: x-string * Example: "SUMMERSALE10OFF" * created_at: <p>The date and time (<a href="https://en.wikipedia.org/wiki/ISO_8601" target="_blank">ISO 8601</a> format) when the discount code was created.</p> * Type: x-string * Example: "2017-03-13T16:09:54-04:00" * updated_at: <p>The date and time (<a href="https://en.wikipedia.org/wiki/ISO_8601" target="_blank">ISO 8601</a> format) when the discount code was updated.</p> * Type: x-string * Example: "2017-03-13T16:09:54-04:00" * id: The ID for the discount code. * Type: x-string * Example: 9808080986 * price_rule_id: The ID for the price rule that this discount code belongs to. * Type: x-string * Example: 423748927342 * usage_count: The number of times that the discount code has been redeemed. * Type: x-string * Example: 3 * errors: An array of errors that occurred when retrieving a list of discount codes for a discount code creation job. * Type: x-string * Example: {"code"=>["must be unique. Please try a different code."]} ## Creates a discount code <p>Creates a discount code.</p> <p>The <code>price_rule_id</code> path parameter is the ID of the price rule that the discount code will belong to. This is required because each discount code must belong to a price rule.</p> ### Endpoint /admin/api/#{api_version}/price_rules/{price_rule_id}/discount_codes.json (POST) ### Parameters * api_version (required): * price_rule_id (required): ### Responses #### 201 Creates a discount code Examples: ##### Create a discount code Request: ``` POST /admin/api/unstable/price_rules/507328175/discount_codes.json {"discount_code":{"code":"SUMMERSALE10OFF"}} ``` Response: ``` HTTP/1.1 201 Created {"discount_code":{"id":1057371196,"price_rule_id":507328175,"code":"SUMMERSALE10OFF","usage_count":0,"created_at":"2025-01-02T11:31:50-05:00","updated_at":"2025-01-02T11:31:50-05:00"}} ``` ## Retrieves a list of discount codes <p>Retrieve a list of discount codes. <strong>Note:</strong> This endpoint implements pagination by using links that are provided in the response header. To learn more, refer to <a href='/api/usage/pagination-rest'>Make paginated requests to the REST Admin API</a>.</p> <p>The <code>price_rule_id</code> path parameter is the ID of the price rule that the discount codes belongs to.</p> ### Endpoint /admin/api/#{api_version}/price_rules/{price_rule_id}/discount_codes.json (GET) ### Parameters * api_version (required): * price_rule_id (required): ### Responses #### 200 Retrieves a list of discount codes Examples: ##### Retrieve a list of all discount codes Request: ``` GET /admin/api/unstable/price_rules/507328175/discount_codes.json ``` Response: ``` HTTP/1.1 200 OK {"discount_codes":[{"id":507328175,"price_rule_id":507328175,"code":"SUMMERSALE10OFF","usage_count":0,"created_at":"2025-01-02T11:29:59-05:00","updated_at":"2025-01-02T11:29:59-05:00"}]} ``` ## Updates an existing discount code <p>Updates an existing discount code.</p> <p>The <code>price_rule_id</code> path parameter is the ID of the price rule that the discount code belongs to. This is required because each discount code must belong to a price rule.</p> <p>The <code>discount_code_id</code> path parameter is the ID of the discount code to update for the associated price rule.</p> ### Endpoint /admin/api/#{api_version}/price_rules/{price_rule_id}/discount_codes/{discount_code_id}.json (PUT) ### Parameters * api_version (required): * discount_code_id (required): * price_rule_id (required): ### Responses #### 200 Updates an existing discount code Examples: ##### Update the code for a discount Request: ``` PUT /admin/api/unstable/price_rules/507328175/discount_codes/507328175.json {"discount_code":{"id":507328175,"code":"WINTERSALE20OFF"}} ``` Response: ``` HTTP/1.1 200 OK {"discount_code":{"id":507328175,"price_rule_id":507328175,"code":"WINTERSALE20OFF","usage_count":0,"created_at":"2025-01-02T11:29:59-05:00","updated_at":"2025-01-02T11:31:48-05:00"}} ``` ## Retrieves a single discount code <p>Retrieves a single discount code.</p> <p>The <code>price_rule_id</code> path parameter is the ID of the price rule that the discount code belongs to. This is required because each discount code must belong to a price rule.</p> <p>The <code>discount_code_id</code> path parameter is the ID of the discount code to retrieve for the associated price rule.</p> ### Endpoint /admin/api/#{api_version}/price_rules/{price_rule_id}/discount_codes/{discount_code_id}.json (GET) ### Parameters * api_version (required): * discount_code_id (required): * price_rule_id (required): ### Responses #### 200 Retrieves a single discount code Examples: ##### Retrieve a single discount code Request: ``` GET /admin/api/unstable/price_rules/507328175/discount_codes/507328175.json ``` Response: ``` HTTP/1.1 200 OK {"discount_code":{"id":507328175,"price_rule_id":507328175,"code":"SUMMERSALE10OFF","usage_count":0,"created_at":"2025-01-02T11:29:59-05:00","updated_at":"2025-01-02T11:29:59-05:00"}} ``` ## Deletes a discount code <p>Deletes a discount code.</p> <p>The <code>price_rule_id</code> path parameter is the ID of the price rule that the discount code belongs to. This is required because each discount code must belong to a price rule.</p> <p>The <code>discount_code_id</code> path parameter is the ID of the discount code to delete for the associated price rule.</p> ### Endpoint /admin/api/#{api_version}/price_rules/{price_rule_id}/discount_codes/{discount_code_id}.json (DELETE) ### Parameters * api_version (required): * discount_code_id (required): * price_rule_id (required): ### Responses #### 204 Deletes a discount code Examples: ##### Delete a discount code Request: ``` DELETE /admin/api/unstable/price_rules/507328175/discount_codes/507328175.json ``` Response: ``` HTTP/1.1 204 No Content ``` ## Retrieves the location of a discount code <p>Retrieves the location of a discount code.</p> <p>The discount code's location is returned in the location header, not in the DiscountCode object itself. Depending on your HTTP client, the location of the discount code might follow the location header automatically.</p> ### Endpoint /admin/api/#{api_version}/discount_codes/lookup.json?code=SUMMERSALE10OFF (GET) ### Parameters * api_version (required): * code (required): Retrieves the location of a discount code by code name. ### Responses #### 303 Retrieves the location of a discount code Examples: ##### Search for a discount code Request: ``` GET /admin/api/unstable/discount_codes/lookup.json ``` Response: ``` HTTP/1.1 303 See Other ``` ## Retrieves a count of discount codes for a shop Retrieves a count of discount codes for a shop ### Endpoint /admin/api/#{api_version}/discount_codes/count.json (GET) ### Parameters * api_version (required): * times_used: Show discount codes with times used. * times_used_max: Show discount codes used greater than or equal to this value. * times_used_min: Show discount codes used less than or equal to this value. ### Responses #### 200 Retrieves a count of discount codes for a shop Examples: ##### Retrieve a count of discount codes for a shop Request: ``` GET /admin/api/unstable/discount_codes/count.json ``` Response: ``` HTTP/1.1 200 OK {"count":2} ``` ## Creates a discount code creation job <p>Creates a discount code creation job.</p> <p>The batch endpoint can be used to asynchronously create up to 100 discount codes in a single request. It enqueues and returns a <code>discount_code_creation</code> object that can be monitored for completion. You can enqueue a single creation job per a shop and you can't enqueue more until the job completes.</p> <p>The <code>price_rule_id</code> path parameter is the ID of the price rule that the discount code will belong to. This is required because each discount code must belong to a price rule.</p> <p>Response fields that are specific to the batch endpoint include: <ul> <li><code>status</code>: The state of the discount code creation job. Possible values are: <ul> <li><code>queued</code>: The job is acknowledged, but not started.</li> <li><code>running</code>: The job is in process.</li> <li><code>completed</code>: The job has finished.</li> </ul> <li><code>codes_count</code>: The number of discount codes to create.</li> <li><code>imported_count</code>: The number of discount codes created successfully.</li> <li><code>failed_count</code>: The number of discount codes that were not created successfully. Unsuccessful attempts will retry up to three times.</li> <li><code>logs</code>: A report that specifies when no discount codes were created because the provided data was invalid. Example responses: <ul> <li>"Price rule target selection can't be blank"</li> <li>"Price rule allocation method can't be blank"</li> </ul> </li> </ul> </p> ### Endpoint /admin/api/#{api_version}/price_rules/{price_rule_id}/batch.json (POST) ### Parameters * api_version (required): * price_rule_id (required): ### Responses #### 201 Creates a discount code creation job Examples: ##### Create a discount code creation job Request: ``` POST /admin/api/unstable/price_rules/507328175/batch.json {"discount_codes":[{"code":"SUMMER1"},{"code":"SUMMER2"},{"code":"SUMMER3"}]} ``` Response: ``` HTTP/1.1 201 Created {"discount_code_creation":{"id":989355119,"price_rule_id":507328175,"started_at":null,"completed_at":null,"created_at":"2025-01-02T11:31:50-05:00","updated_at":"2025-01-02T11:31:50-05:00","status":"queued","codes_count":3,"imported_count":0,"failed_count":0,"logs":[]}} ``` ## Retrieves a discount code creation job <p>Retrieves a discount code creation job</p> <p>The <code>price_rule_id</code> path parameter is the ID of the price rule that the discount code creation job was ran for. This is required because each discount code creation job is associated to a price rule.</p> <p>The <code>batch_id</code> path parameter is the ID of the discount code creation job for the associated price rule.</p> ### Endpoint /admin/api/#{api_version}/price_rules/{price_rule_id}/batch/{batch_id}.json (GET) ### Parameters * api_version (required): * batch_id (required): * price_rule_id (required): ### Responses #### 200 Retrieves a discount code creation job Examples: ##### Retrieve a discount code creation job Request: ``` GET /admin/api/unstable/price_rules/507328175/batch/173232803.json ``` Response: ``` HTTP/1.1 200 OK {"discount_code_creation":{"id":173232803,"price_rule_id":507328175,"started_at":null,"completed_at":null,"created_at":"2025-01-02T11:29:59-05:00","updated_at":"2025-01-02T11:29:59-05:00","status":"queued","codes_count":3,"imported_count":0,"failed_count":0,"logs":[]}} ``` ## Retrieves a list of discount codes for a discount code creation job <p>Retrieves a list of discount codes for a discount code creation job.</p> <p>The <code>price_rule_id</code> path parameter is the ID of the price rule that the discount code creation job was ran for. This is required because each discount code creation job is associated to a price rule.</p> <p>The <code>batch_id</code> path parameter is the ID of the discount code creation job for the associated price rule.</p> <p>Discount codes that have been successfully created include a populated <code>id</code> field. Discount codes that encountered errors during the creation process include a populated <code>errors</code> field.</p> ### Endpoint /admin/api/#{api_version}/price_rules/{price_rule_id}/batch/{batch_id}/discount_codes.json (GET) ### Parameters * api_version (required): * batch_id (required): * price_rule_id (required): ### Responses #### 200 Retrieves a list of discount codes for a discount code creation job Examples: ##### Retrieve a list of discount codes for a discount code creation job Request: ``` GET /admin/api/unstable/price_rules/507328175/batch/173232803/discount_codes.json ``` Response: ``` HTTP/1.1 200 OK {"discount_codes":[{"id":null,"code":"foo","errors":{}},{"id":null,"code":"","errors":{}},{"id":null,"code":"bar","errors":{}}]} ```