---
title: "创建DiscountCode"
description: "创建优惠码"
doc_type: openapi
doc_category: openapi
doc_version: v20260901
source_url:
  html: 'https://developer.shopline.com/zh-hans-cn/docs/admin-rest-api/marketing/discount-code/create-discount-code'
  md: 'https://developer.shopline.com/zh-hans-cn/docs/admin-rest-api/marketing/discount-code/create-discount-code.md'
---
# 创建DiscountCode

创建优惠码

- **Base URL:** `https://{handle}.myshopline.com/admin/openapi`
- **Required Scopes:** `write_discounts`
- **API Version:** v20260901
- **API Type:** Admin
- **API Protocol:** RESTful

## Endpoint

```http
POST /sales/price_rules/:price_rule_id/discount_codes.json
```

## Base URL

* `https://{handle}.myshopline.com/admin/openapi`

  * `{handle}`: `handle` ：店铺的唯一标识符，值为店铺域名的前缀。例如，域名为 `open001.myshopline.com` 的店铺，其 `handle` 是 `open001` 。

## Required Permissions

* `write_discounts`

## Request

### Path Parameters

* `price_rule_id`

  string (required)

  折扣码规则id

### Request Headers

* `Content-Type`

  string (required)

  字段值固定为 `application/json; charset=utf-8`。

* `Authorization`

  string (required)

  资源的访问令牌。参考 [应用授权](https://developer.shopline.com/zh-hans-cn/docs/apps/api-instructions-for-use/app-authorization.md?lang=zh-hans-cn) 获取访问令牌，然后将获取到的访问令牌以 Bearer 格式传入。

### Request Body

* `discount_code`

  object

  折扣码

  * `price_rule_id`

    string

    折扣码规则id

  * `code`

    string

    折扣码内容

## Response

### Status Codes

* `200`

  更多状态码信息，参考 [HTTP 状态码](https://developer.shopline.com/zh-hans-cn/docs/apps/api-instructions-for-use/http-status-code.md?lang=zh-hans-cn)。

* `401`

  

* `429`

  

### Response Headers

* `traceId`

  请求的全局唯一标识符。用于追踪请求在系统中的流转，以便于问题发生时进行定位和调试。

### Response Body

#### 200

* `discount_code`

  object

  折扣码实体

  * `create_at`

    string

    折扣码的创建时间

  * `id`

    string

    折扣码的ID

  * `price_rule_id`

    string

    折扣码规则的ID

  * `update_at`

    string

    折扣码的更新时间

  * `usage_count`

    integer

    折扣码的已使用次数

  * `code`

    string

    折扣码内容


#### 401

* `errors`

  string

  错误描述。


#### 429

* `errors`

  string

  错误描述。


## OpenAPI Definition

```json
{
  "description": "创建优惠码",
  "externalDocs": {
    "description": "接口使用的权限点: `write_discounts` ，更多权限点使用信息，请参考:",
    "url": "ec20/3cv5d7wpfgr6a8z5/xabczf5442284vkq?lang=zh-hans-cn",
    "errorTableDescription": ""
  },
  "operationId": "NZxEIBPL",
  "parameters": [
    {
      "name": "price_rule_id",
      "in": "path",
      "description": "折扣码规则id",
      "required": true,
      "style": "simple",
      "schema": {
        "name": "price_rule_id",
        "description": "折扣码规则id",
        "type": "string",
        "example": "6018703930879383908"
      },
      "example": "6018703930879383908",
      "examples": {
        "创建分销折扣码": {
          "value": "6019132058521049481"
        },
        "创建折扣码": {
          "value": "6062017087609511417"
        }
      }
    },
    {
      "name": "Content-Type",
      "in": "header",
      "description": "字段值固定为 `application/json; charset=utf-8`。",
      "required": true,
      "style": "string",
      "schema": {
        "name": "Content-Type",
        "type": "string",
        "example": "application/json; charset=utf-8"
      }
    },
    {
      "name": "Authorization",
      "in": "header",
      "description": "资源的访问令牌。参考 [应用授权](https://developer.shopline.com/zh-hans-cn/docs/apps/api-instructions-for-use/app-authorization.md?lang=zh-hans-cn) 获取访问令牌，然后将获取到的访问令牌以 Bearer 格式传入。",
      "required": true,
      "style": "string",
      "schema": {
        "name": "Authorization",
        "description": "资源的访问令牌。请参考 [应用授权](https://developer.shopline.com/zh-hans-cn/docs/apps/api-instructions-for-use/app-authorization.md?lang=zh-hans-cn) 获取访问令牌，然后将获取到的访问令牌以 Bearer 格式传入",
        "type": "string",
        "example": "Bearer eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhcHBJZCI6MCwiYXBwS2V5IjoiNzU2ZjI1NWE2NjdlZTcwNTk1MzM0ZjVjYzk4NzU5MzBmN2E3NGZjMSIsImV4cCI6MTg3OTQ4MjA3NiwiaXNzIjoieXNvdWwiLCJzZWxsZXJJZCI6IjIwMDAyMzQ4ODAiLCJzdG9yZUlkIjoiMTY3MjM2OTcyOTYwNiIsInRpbWVzdGFtcCI6MTc4NDc4NzY3NjY2MSwidmVyc2lvbiI6IlYyIn0.3ZzbGAH4cLU1QR2VPEvMOQInUDvus-C4amkINSQIPuNVlXsFKj0sAt1c0KnUy9MWUOFV9cHlkf7J90r2y1FzpA"
      },
      "example": "Bearer eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhcHBJZCI6MCwiYXBwS2V5IjoiNzU2ZjI1NWE2NjdlZTcwNTk1MzM0ZjVjYzk4NzU5MzBmN2E3NGZjMSIsImV4cCI6MTg3OTQ4MjA3NiwiaXNzIjoieXNvdWwiLCJzZWxsZXJJZCI6IjIwMDAyMzQ4ODAiLCJzdG9yZUlkIjoiMTY3MjM2OTcyOTYwNiIsInRpbWVzdGFtcCI6MTc4NDc4NzY3NjY2MSwidmVyc2lvbiI6IlYyIn0.3ZzbGAH4cLU1QR2VPEvMOQInUDvus-C4amkINSQIPuNVlXsFKj0sAt1c0KnUy9MWUOFV9cHlkf7J90r2y1FzpA"
    }
  ],
  "requestBody": {
    "content": {
      "application/json": {
        "schema": {
          "type": "object",
          "properties": {
            "discount_code": {
              "name": "discount_code",
              "description": "折扣码",
              "type": "object",
              "properties": {
                "price_rule_id": {
                  "name": "price_rule_id",
                  "description": "折扣码规则id",
                  "type": "string",
                  "example": "6018703930879383908"
                },
                "code": {
                  "name": "code",
                  "description": "折扣码内容",
                  "type": "string",
                  "example": "DISCOUNT_CODE_CONTENT"
                }
              },
              "required": [
                "code"
              ],
              "example": {
                "code": "DISCOUNT_CODE_CONTENT",
                "price_rule_id": "6018703930879383908"
              }
            }
          },
          "example": {
            "discount_code": {
              "price_rule_id": "6018703930879383908",
              "code": "DISCOUNT_CODE_CONTENT"
            }
          }
        },
        "examples": {
          "创建分销折扣码": {
            "value": {
              "discount_code": {
                "code": "AFFILLIATE_DISCOUNT_CODE",
                "price_rule_id": "6019132058521049481"
              }
            }
          },
          "创建折扣码": {
            "value": {
              "discount_code": {
                "code": "DISCOUNT_CODE_CONTENT_1",
                "price_rule_id": "6062017087609511417"
              }
            }
          }
        }
      }
    },
    "required": true
  },
  "responses": {
    "200": {
      "description": "更多状态码信息，参考 [HTTP 状态码](https://developer.shopline.com/zh-hans-cn/docs/apps/api-instructions-for-use/http-status-code.md?lang=zh-hans-cn)。",
      "headers": {
        "traceId": {
          "description": "请求的全局唯一标识符。用于追踪请求在系统中的流转，以便于问题发生时进行定位和调试。"
        }
      },
      "content": {
        "application/json": {
          "schema": {
            "type": "object",
            "properties": {
              "discount_code": {
                "name": "discount_code",
                "description": "折扣码实体",
                "type": "object",
                "properties": {
                  "create_at": {
                    "name": "create_at",
                    "description": "折扣码的创建时间",
                    "type": "string",
                    "example": "2023-07-12T00:08:02+08:00"
                  },
                  "id": {
                    "name": "id",
                    "description": "折扣码的ID",
                    "type": "string",
                    "example": "6019053959104174638"
                  },
                  "price_rule_id": {
                    "name": "price_rule_id",
                    "description": "折扣码规则的ID",
                    "type": "string",
                    "example": "6018703930879383908"
                  },
                  "update_at": {
                    "name": "update_at",
                    "description": "折扣码的更新时间",
                    "type": "string",
                    "example": "2023-07-12T00:08:02+08:00"
                  },
                  "usage_count": {
                    "name": "usage_count",
                    "description": "折扣码的已使用次数",
                    "type": "integer",
                    "example": 0
                  },
                  "code": {
                    "name": "code",
                    "description": "折扣码内容",
                    "type": "string",
                    "example": "DISCOUNT_CODE_CONTENT"
                  }
                },
                "example": {
                  "create_at": "2023-07-12T00:08:02+08:00",
                  "id": "6019053959104174638",
                  "price_rule_id": "6018703930879383908",
                  "update_at": "2023-07-12T00:08:02+08:00",
                  "usage_count": 0,
                  "code": "DISCOUNT_CODE_CONTENT"
                }
              }
            },
            "example": {
              "discount_code": {
                "create_at": "2023-07-12T00:08:02+08:00",
                "id": "6019053959104174638",
                "price_rule_id": "6018703930879383908",
                "update_at": "2023-07-12T00:08:02+08:00",
                "usage_count": 0,
                "code": "DISCOUNT_CODE_CONTENT"
              }
            }
          },
          "examples": {
            "创建折扣码": {
              "value": {
                "discount_code": {
                  "code": "DISCOUNT_CODE_CONTENT_1",
                  "create_at": "2023-08-10T15:45:03+08:00",
                  "id": "6062034195202449185",
                  "price_rule_id": "6062017087609511417",
                  "update_at": "2023-08-10T15:45:03+08:00",
                  "usage_count": 0
                }
              }
            },
            "创建分销折扣码": {
              "value": {
                "discount_code": {
                  "usage_count": 0,
                  "code": "AFFILLIATE_DISCOUNT_CODE",
                  "create_at": "2023-08-10T16:23:45+08:00",
                  "id": "6062073148844549600",
                  "price_rule_id": "6019132058521049481",
                  "update_at": "2023-08-10T16:23:45+08:00"
                }
              }
            }
          }
        }
      }
    },
    "401": {
      "headers": {
        "traceId": {
          "description": "请求的全局唯一标识符。用于追踪请求在系统中的流转，以便于问题发生时进行定位和调试。"
        }
      },
      "content": {
        "application/json": {
          "schema": {
            "type": "object",
            "properties": {
              "errors": {
                "description": "错误描述。",
                "type": "string"
              }
            },
            "required": [
              "errors"
            ]
          }
        }
      }
    },
    "429": {
      "headers": {
        "traceId": {
          "description": "请求的全局唯一标识符。用于追踪请求在系统中的流转，以便于问题发生时进行定位和调试。"
        }
      },
      "content": {
        "application/json": {
          "schema": {
            "type": "object",
            "properties": {
              "errors": {
                "description": "错误描述。",
                "type": "string"
              }
            },
            "required": [
              "errors"
            ]
          }
        }
      }
    }
  },
  "security": [
    {
      "oauth2": []
    }
  ],
  "method": "post",
  "path": "/sales/price_rules/:price_rule_id/discount_codes.json",
  "servers": [
    {
      "url": "https://{handle}.myshopline.com/admin/openapi",
      "variables": {
        "handle": {
          "default": "openapiceshidianpu",
          "description": "`handle` ：店铺的唯一标识符，值为店铺域名的前缀。例如，域名为 `open001.myshopline.com` 的店铺，其 `handle` 是 `open001` 。"
        }
      }
    }
  ],
  "info": {
    "title": "创建DiscountCode",
    "version": "v20260901"
  },
  "authIndexes": [
    "write_discounts"
  ]
}
```