0

List Blogs

by
Published Nov 8, 2023

List the online store's blogs, optionally filtered with Shopify's search syntax.

Script shopify Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 2 hours ago
1
//native
2

3
/**
4
 * List Blogs
5
 * List the online store's blogs, optionally filtered with Shopify's search syntax (e.g. `title:News handle:news`). Returns one page; pass pageInfo.endCursor as after for the next. Needs the read_content scope.
6
 */
7
export async function main(
8
  auth: RT.Shopify,
9
  query: string | undefined,
10
  sort_key: "TITLE" | "HANDLE" | "ID" | undefined,
11
  reverse: boolean | undefined,
12
  first: number | undefined,
13
  after: string | undefined
14
) {
15
  const response = await fetch(
16
    `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`,
17
    {
18
      method: "POST",
19
      headers: {
20
        "X-Shopify-Access-Token": auth.token,
21
        "Content-Type": "application/json",
22
        Accept: "application/json",
23
      },
24
      body: JSON.stringify({
25
        query: `query ($first: Int!, $after: String, $query: String, $sortKey: BlogSortKeys, $reverse: Boolean) { blogs(first: $first, after: $after, query: $query, sortKey: $sortKey, reverse: $reverse) { nodes { id title handle commentPolicy tags templateSuffix createdAt updatedAt articlesCount { count } } pageInfo { hasNextPage endCursor } } }`,
26
        variables: {
27
          first: first || 50,
28
          after: after || undefined,
29
          query: query || undefined,
30
          sortKey: sort_key || undefined,
31
          reverse: reverse || undefined,
32
        },
33
      }),
34
    }
35
  )
36

37
  if (!response.ok) {
38
    throw new Error(`${response.status} ${await response.text()}`)
39
  }
40

41
  const { data, errors } = await response.json()
42
  if (errors) {
43
    throw new Error(JSON.stringify(errors))
44
  }
45

46
  return data.blogs
47
}
48

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 528 days ago
    1
    type Shopify = {
    2
      token: string;
    3
      store_name: string;
    4
    };
    5
    /**
    6
     * Retrieve a list of all blogs
    7
     * Retrieve a list of all blogs. Note: As of version 2019-10, this endpoint implements pagination by using links that are provided in the response header. Sending the page parameter will return an error. To learn more, see Making requests to paginated REST Admin API endpoints.
    8
     */
    9
    export async function main(
    10
      auth: Shopify,
    11
      api_version: string = "2023-10",
    12
      limit: string | undefined,
    13
      since_id: string | undefined,
    14
      handle: string | undefined,
    15
      fields: string | undefined
    16
    ) {
    17
      const url = new URL(
    18
        `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/blogs.json`
    19
      );
    20
      for (const [k, v] of [
    21
        ["limit", limit],
    22
        ["since_id", since_id],
    23
        ["handle", handle],
    24
        ["fields", fields],
    25
      ]) {
    26
        if (v !== undefined && v !== "") {
    27
          url.searchParams.append(k, v);
    28
        }
    29
      }
    30
      const response = await fetch(url, {
    31
        method: "GET",
    32
        headers: {
    33
          "X-Shopify-Access-Token": auth.token,
    34
        },
    35
        body: undefined,
    36
      });
    37
      if (!response.ok) {
    38
        const text = await response.text();
    39
        throw new Error(`${response.status} ${text}`);
    40
      }
    41
      return await response.json();
    42
    }
    43