0

Creates a redirect

Published Nov 8, 2023

Creates a redirect. When you provide a full URL as the value of the path property, it will be saved as an absolute path without the domain. For example, "path": "http://www.johns-apparel.com/springwear" will be saved as "path": "springwear".

Script shopify
  • Submitted by hugo697 Typescript (fetch-only)
    Created 566 days ago
    1
    type Shopify = {
    2
      token: string;
    3
      store_name: string;
    4
    };
    5
    /**
    6
     * Creates a redirect
    7
     * Creates a redirect. When you provide a full URL as the value of the path property, it will be saved as an absolute path without the domain. For example, "path": "http://www.johns-apparel.com/springwear" will be saved as "path": "springwear".
    8
     */
    9
    export async function main(
    10
      auth: Shopify,
    11
      api_version: string = "2023-10",
    12
      body: {
    13
        redirect?: { body?: string; [k: string]: unknown };
    14
        [k: string]: unknown;
    15
      }
    16
    ) {
    17
      const url = new URL(
    18
        `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/redirects.json`
    19
      );
    20
    
    
    21
      const response = await fetch(url, {
    22
        method: "POST",
    23
        headers: {
    24
          "Content-Type": "application/json",
    25
          "X-Shopify-Access-Token": auth.token,
    26
        },
    27
        body: JSON.stringify(body),
    28
      });
    29
      if (!response.ok) {
    30
        const text = await response.text();
    31
        throw new Error(`${response.status} ${text}`);
    32
      }
    33
      return await response.json();
    34
    }
    35