---
openapi: 3.0.3
info:
  title: Braze REST API – Create preference center
  description: |
    ## Prerequisites

    To use this endpoint, you'll need an [API key]({{site.baseurl}}/api/basics#rest-api-key-permissions) with the `preference_center.update` permission.

    ## Rate limit

    This endpoint has a rate limit of 10 requests per minute, per workspace, as documented in [API rate limits]({{site.baseurl}}/api/api_limits/).
  version: 1.0.0
  contact:
    name: Braze Support
    url: https://www.braze.com/docs/braze_support/
  license:
    name: Braze Documentation
    url: https://www.braze.com/docs/api/home/
servers:
  - url: https://rest.iad-01.braze.com
    description: US-01
  - url: https://rest.iad-02.braze.com
    description: US-02
  - url: https://rest.iad-03.braze.com
    description: US-03
  - url: https://rest.iad-04.braze.com
    description: US-04
  - url: https://rest.iad-05.braze.com
    description: US-05
  - url: https://rest.iad-06.braze.com
    description: US-06
  - url: https://rest.iad-07.braze.com
    description: US-07
  - url: https://rest.iad-08.braze.com
    description: US-08
  - url: https://rest.us-10.braze.com
    description: US-10
  - url: https://rest.fra-01.braze.eu
    description: EU-01
  - url: https://rest.fra-02.braze.eu
    description: EU-02
  - url: https://rest.au-01.braze.com
    description: AU-01
  - url: https://rest.id-01.braze.com
    description: ID-01
  - url: https://rest.jp-01.braze.com
    description: JP-01
  - url: https://rest.kr-01.braze.com
    description: KR-01
security:
- BearerAuth: []
# cspell:ignore onchange
paths:
  "/preference_center/v1":
    post:
      summary: Create preference center
      description: |
        ## Liquid tags

        Refer to the following Liquid tags that can be included in your HTML to generate a user's subscription state on the preference center page.

        ### User subscription state

        | Liquid | Description |
        | --------- | ---------|
        |`{{subscribed_state.${email_global}}}`| Get the global email subscribed state for the user (such as "opted_in", "subscribed", or "unsubscribed". |
        |`{{subscribed_state.${<subscription_group_id>}}}`| Get the subscribed state of the specified subscription group for the user (such as "subscribed" or "unsubscribed"). |

        ### Form inputs and action

        | Liquid | Description |
        | --------- | ---------|
        |`{% form_field_name :email_global_state %}`| Indicates that a specific form input element corresponds to the user's global email subscribed state. The user's selection state should be "opted_in", "subscribed", or "unsubscribed" when the form is submitted with selection data for the global email subscribed state. If it's a checkbox, the user will either be "opted_in" or "unsubscribed". For a hidden input, the "subscribed" state will also be valid. |
        |`{% form_field_name :subscription_group <subscription_group_id> %}`| Indicates that a specific form input element corresponds to a given subscription group. The user's selection state should be either "subscribed" or "unsubscribed" when the form is submitted with selection data for a specific subscription group. |
        |`{{preference_center_submit_url}}`| Generates URL for form submission. |
      operationId: post_preference_center_v1_post_create_preference_center
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreatePreferenceCenterRequest'
            examples:
              create_preference_center:
                summary: Create preference center
                value:
                  name: MyPreferenceCenter2022-09-22
                  preference_center_title: Example Preference Center Title
                  preference_center_page_html: HTML for preference center here
                  confirmation_page_html: HTML here with a message to users here
                  state: active
                  options:
                    meta-viewport-content: width=device-width, initial-scale=2
                    links-tags:
                      - rel: icon
                        type: image/png
                        href: https://www.example.com/favicon.png
              html_with_form_inputs:
                summary: HTML with form inputs
                description: The following sample request body shows an example for HTML with form inputs.
                value:
                  name: Email_Preferences
                  preference_center_title: Email Preferences
                  preference_center_page_html: |
                    <!doctype html>
                    <html lang="en">
                      <head>
                        <meta name="robots" content="noindex" />
                        <title>Email Preferences</title>
                        <script type="text/javascript">
                          window.onload = () => {
                            const globalUnsubscribed = '{{subscribed_state.${email_global}}}' == "unsubscribed";
                            const globalSubscribedValue = '{{subscribed_state.${email_global}}}' == "opted_in" ? "opted_in" : "subscribed";
                            const idStates = [
                              // input format: [API_ID, '{{subscribed_state.${API_ID}}}' == "subscribed"][]
                              ['3d2ae07a-f2ff-4318-bdff-e394f2d3a4ec', '{{subscribed_state.${3d2ae07a-f2ff-4318-bdff-e394f2d3a4ec}}}' == 'subscribed'],['7d89bdc3-4aa1-4592-8b8a-4c8b7161c875', '{{subscribed_state.${7d89bdc3-4aa1-4592-8b8a-4c8b7161c875}}}' == 'subscribed'],['5444d32e-2815-4258-964c-b9690d4ccb94', '{{subscribed_state.${5444d32e-2815-4258-964c-b9690d4ccb94}}}' == 'subscribed']
                            ];

                            const setState = (id, subscribed) => {
                              document.querySelector(`#checkbox-${id}`).checked = subscribed;
                              document.querySelector(`#value-${id}`).value = subscribed ? "subscribed" : "unsubscribed";
                            };
                            const setGlobal = (unsubscribed) => {
                              document.querySelector(`#checkbox-global`).checked = unsubscribed;
                              document.querySelector(`#value-global`).value = unsubscribed ? "unsubscribed" : globalSubscribedValue;
                              idStates.forEach(([id]) => {
                                document.querySelector(`#checkbox-${id}`).disabled = unsubscribed;
                              });
                            };

                            idStates.forEach(([id, state]) => {
                              setState(id, state);
                              document.querySelector(`#checkbox-${id}`).onchange = ((e) => {
                                setState(id, e.target.checked);
                              });
                            });
                            setGlobal(globalUnsubscribed);
                            document.querySelector(`#checkbox-global`).onchange = ((e) => {
                              setGlobal(e.target.checked);
                            });
                          };
                        </script>
                        <style>
                          body {
                            background: #fff;
                            margin: 0;
                          }
                          @media (max-width: 600px) {
                            .main-container {
                              margin-top: 0;
                              width: 100%;
                            }
                            .main-container .content .email-input {
                              width: 100%;
                            }
                          }
                        </style>
                      </head>
                      <body class="vsc-initialized" style="margin: 0" bgcolor="#fff">
                        <div
                          class="main-container"
                          style="
                            background-color: #fff;
                            color: #333335;
                            font-family:
                              Aribau Grotesk Regular,
                              helvetica,
                              arial,
                              sans-serif;
                            margin-left: auto;
                            margin-right: auto;
                            margin-top: 30px;
                            width: 600px;
                            padding: 15px 0 5px;
                          "
                        >
                          <div class="content" style="margin-left: 20px; margin-right: 20px">

                            <div>
                              <h1
                                style="color: #3accdd; font-size: 27px; font-weight: 400; margin-bottom: 40px; margin-top: 0"
                                align="center"
                              >
                                Manage Email Preferences
                              </h1>
                              <p class="intro-text" style="font-size: 14px; margin-bottom: 20px" align="center">
                                Select the emails that you want to receive.
                              </p>
                            </div>

                            <form action="{{ preference_center_submit_url }}" method="post" accept-charset="UTF-8">
                              <div>
                                <h3 style="font-size: 15px; margin-bottom: 15px; margin-left: 5px; margin-top: 40px">
                                  Email Address: <span class="displayed-email" style="font-weight: 400">{{${email_address}}}</span>
                                </h3>
                              </div>
                              <div class="subscription-groups-holder" style="margin-bottom: 20px"><div class="row" style="border-top-width: 1px; border-top-color: #dddde2; border-top-style: solid; background-color: #fff; padding: 15px 10px 14px;border-bottom: 1px solid rgb(221, 221, 226);">
                      <label style="color: #27368f; cursor: pointer; font-size: 15px; font-weight: 700;">
                        <input type="checkbox" id="checkbox-3d2ae07a-f2ff-4318-bdff-e394f2d3a4ec" class="sub_group" style="margin-right: 4px;">
                        <input type="hidden" name="{% form_field_name :subscription_group 3d2ae07a-f2ff-4318-bdff-e394f2d3a4ec %}" id="value-3d2ae07a-f2ff-4318-bdff-e394f2d3a4ec" />
                        Sub Group 1
                      </label>
                      <p class="subscription-group" style="font-size: 13px; line-height: 1.4em; min-height: 20px; padding-right: 20px; margin: 0 0 3px 23px;">

                      </p>
                    </div>
                    <div class="row" style="border-top-width: 1px; border-top-color: #dddde2; border-top-style: solid; background-color: #fff; padding: 15px 10px 14px;border-bottom: 1px solid rgb(221, 221, 226);">
                      <label style="color: #27368f; cursor: pointer; font-size: 15px; font-weight: 700;">
                        <input type="checkbox" id="checkbox-7d89bdc3-4aa1-4592-8b8a-4c8b7161c875" class="sub_group" style="margin-right: 4px;">
                        <input type="hidden" name="{% form_field_name :subscription_group 7d89bdc3-4aa1-4592-8b8a-4c8b7161c875 %}" id="value-7d89bdc3-4aa1-4592-8b8a-4c8b7161c875" />
                        Sub Group 2
                      </label>
                      <p class="subscription-group" style="font-size: 13px; line-height: 1.4em; min-height: 20px; padding-right: 20px; margin: 0 0 3px 23px;">

                      </p>
                    </div>
                    <div class="row" style="border-top-width: 1px; border-top-color: #dddde2; border-top-style: solid; background-color: #fff; padding: 15px 10px 14px;border-bottom: 1px solid rgb(221, 221, 226);">
                      <label style="color: #27368f; cursor: pointer; font-size: 15px; font-weight: 700;">
                        <input type="checkbox" id="checkbox-5444d32e-2815-4258-964c-b9690d4ccb94" class="sub_group" style="margin-right: 4px;">
                        <input type="hidden" name="{% form_field_name :subscription_group 5444d32e-2815-4258-964c-b9690d4ccb94 %}" id="value-5444d32e-2815-4258-964c-b9690d4ccb94" />
                        Sub Group 3
                      </label>
                      <p class="subscription-group" style="font-size: 13px; line-height: 1.4em; min-height: 20px; padding-right: 20px; margin: 0 0 3px 23px;">

                      </p>
                    </div>
                    </div>

                              <div class="unsub-all" style="cursor: pointer; font-size: 13px; margin-bottom: 20px" align="center">
                                <label>
                                  <input type="checkbox" id="checkbox-global" />
                                  <input
                                    type="hidden"
                                    id="value-global"
                                    name="{% form_field_name :email_global_state %}"
                                  />
                                  <i> Unsubscribe from all of the above types of emails </i>
                                </label>
                              </div>

                              <div>
                                <input
                                  class="save"
                                  type="submit"
                                  value="Save"
                                  style="
                                    background-color: rgb(71, 204, 163);
                                    color: #fff;
                                    cursor: pointer;
                                    display: block;
                                    font-size: 16px;
                                    text-align: center;
                                    text-decoration: none;
                                    width: 200px;
                                    margin: 0 auto 20px;
                                    padding: 12px;
                                    border-style: none;
                                  "
                                />
                              </div>
                            </form>
                          </div>
                        </div>
                      </body>
                    </html>
                  confirmation_page_html: HTML here with a message to users here
      responses:
        "201":
          description: Created. The request succeeded and the Preference Center was created. No action needed.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/CreatePreferenceCenterResponse'
              examples:
                success:
                  summary: Create preference center
                  value:
                    preference_center_api_id: preference_center_api_id_example
                    liquid_tag: '{{preference_center.${MyPreferenceCenter2022-09-22}}}'
                    created_at: "2022-09-22T18:28:07+00:00"
                    message: success
        "400":
          description: |
            Bad request. To resolve it:

            - **The request is missing a required parameter or violates a constraint:** Correct the request per the details in the error message (`400 Bad request. Details: …`).
            - **The `name` contains characters other than letters, numbers, hyphens, and underscores:** Use a `name` that only contains letters, numbers, hyphens, and underscores, and doesn't have spaces.
            - **A preference center with the same `name` already exists in the workspace:** Use a `name` that's unique in the workspace.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              examples:
                missing_name:
                  summary: Missing required parameter
                  value:
                    error: '400 Bad request. Details: name is missing'
                invalid_name_format:
                  summary: Invalid name format
                  value:
                    error: '400 Bad request. Details: name can only contain alphanumeric characters (A-Z, a-z, 0-9), dashes (-), and underscores (_)'
                name_not_unique:
                  summary: Name already in use
                  value:
                    error: '400 Bad request. Details: Preference Center name must be unique in workspace'
        "401":
          description: |
            Unauthorized. The REST API key is missing, malformed, or sent to the wrong REST endpoint. Send the key as `Authorization: Bearer YOUR_REST_API_KEY` to the correct [REST endpoint]({{site.baseurl}}/api/basics/#endpoints). For more causes, see [Errors and responses]({{site.baseurl}}/api/errors/#fatal-errors).
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              examples:
                invalid_api_key:
                  summary: Invalid API key
                  value:
                    error: Error authenticating credentials
        "403":
          description: Access denied. The REST API key doesn't have the required permission. Use a REST API key that has the `preference_center.update` permission.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              examples:
                access_denied:
                  summary: Missing permission
                  value:
                    error: You do not have permission to access this resource
        "429":
          description: |
            Rate limited. You exceeded the rate limit for this endpoint. Slow your request rate and retry with exponential backoff. See [API rate limits]({{site.baseurl}}/api/api_limits/). The error message says how many seconds to wait before you retry.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              examples:
                over_rate_limit:
                  summary: Over rate limit
                  value:
                    error: 429 Try again in 30 seconds
        "500":
          description: |
            Internal server error. An unexpected error occurred on the Braze server. Retry with exponential backoff. If the error persists, contact [Support]({{site.baseurl}}/braze_support/).
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              examples:
                internal_server_error:
                  summary: Internal server error
                  value:
                    error: 500 Internal Server Error
        5XX:
          description: |
            Internal server error. An unexpected error occurred on the Braze server. Retry with exponential backoff. If the error persists, contact [Support]({{site.baseurl}}/braze_support/).
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
components:
  securitySchemes:
    BearerAuth:
      type: http
      scheme: bearer
      bearerFormat: REST API key
      description: |
        REST API keys are created in the Braze dashboard at **Settings → APIs and
        Identifiers → API Keys**. The key must include the `preference_center.update` permission.
        Pass the key as `Authorization: Bearer YOUR_REST_API_KEY`.
  schemas:
    LinkTag:
      title: Link tag
      type: object
      required: [rel, href]
      properties:
        rel:
          type: string
          enum: [icon, shortcut icon, apple-touch-icon]
          description: One of the following "icon", "shortcut icon", or "apple-touch-icon".
          example: icon
        type:
          type: string
          enum: [image/png, image/svg, image/gif, image/x-icon, image/svg+xml, mask-icon]
          description: Valid values include "image/png", "image/svg", "image/gif", "image/x-icon", "image/svg+xml", "mask-icon".
          example: image/png
        sizes:
          type: string
        color:
          type: string
          description: Use when type="mask-icon".
        href:
          type: string
          example: https://www.example.com/favicon.png
    PreferenceCenterOptions:
      title: Preference center options
      type: object
      description: |
        Attributes:

        `meta-viewport-content`: When present, a `viewport` meta tag will be added to the page with `content= <value of attribute>`.

        `links-tags`: Set a favicon for the page. When set, a `<link>` tag with a rel attribute is added to the page.
      properties:
        meta-viewport-content:
          type: string
          description: Only the `content` value of the meta tag.
          example: width=device-width, initial-scale=2
        links-tags:
          type: array
          items:
            $ref: '#/components/schemas/LinkTag'
    CreatePreferenceCenterRequest:
      title: Create preference center request
      type: object
      required: [name, preference_center_page_html, confirmation_page_html]
      properties:
        name:
          type: string
          pattern: '^[A-Za-z0-9_-]+$'
          description: |
            The name of the preference center that meets the following requirements:

            - Only contains letters, numbers, hyphens, and underscores
            - Does not have spaces

            The preference center name can't be edited after it's created.
          example: MyPreferenceCenter2022-09-22
        preference_center_title:
          type: string
          description: The title for the preference center and confirmation pages. If a title is not specified, the title of the pages will default to "Preference Center".
          example: Example Preference Center Title
        preference_center_page_html:
          type: string
          description: The HTML for the preference center page. For the Liquid tags you can include in it, see [Liquid tags](#liquid-tags).
          example: HTML for preference center here
        confirmation_page_html:
          type: string
          description: The HTML for the confirmation page.
          example: HTML here with a message to users here
        state:
          type: string
          enum: [active, draft]
          default: active
          description: Choose `active` or `draft`. Defaults to `active` if not specified.
          example: active
        options:
          $ref: '#/components/schemas/PreferenceCenterOptions'
    CreatePreferenceCenterResponse:
      title: Create preference center response
      type: object
      properties:
        preference_center_api_id:
          type: string
          description: The ID for your preference center.
          example: preference_center_api_id_example
        liquid_tag:
          type: string
          description: The Liquid tag for this preference center.
          example: '{{preference_center.${MyPreferenceCenter2022-09-22}}}'
        created_at:
          type: string
          description: When the preference center was created, in ISO 8601 format.
          example: "2022-09-22T18:28:07+00:00"
        message:
          type: string
          example: success
    ErrorResponse:
      title: Error
      type: object
      properties:
        error:
          type: string
          description: The error message describing why the request failed.
