0

List Articles

by
Published Nov 8, 2023

List blog articles across the store or in one blog, 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
export type DynSelect_blog_id = string
4

5
export async function blog_id(auth: RT.Shopify) {
6
  const response = await fetch(
7
    `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`,
8
    {
9
      method: "POST",
10
      headers: {
11
        "X-Shopify-Access-Token": auth.token,
12
        "Content-Type": "application/json",
13
        Accept: "application/json",
14
      },
15
      body: JSON.stringify({
16
        query: `query { blogs(first: 250, sortKey: TITLE) { nodes { id title } } }`,
17
      }),
18
    }
19
  )
20
  if (!response.ok) {
21
    throw new Error(`${response.status} ${await response.text()}`)
22
  }
23
  const { data, errors } = await response.json()
24
  if (errors) {
25
    throw new Error(JSON.stringify(errors))
26
  }
27
  return data.blogs.nodes.map((b: { id: string; title: string }) => ({
28
    value: b.id,
29
    label: b.title,
30
  }))
31
}
32

33
/**
34
 * List Articles
35
 * List blog articles, from one blog or all of them, optionally filtered with Shopify's search syntax (e.g. `tag:news author:Jane published_status:published`). Returns one page; pass pageInfo.endCursor as after for the next. Needs the read_content scope.
36
 */
37
export async function main(
38
  auth: RT.Shopify,
39
  blog_id: DynSelect_blog_id | undefined,
40
  query: string | undefined,
41
  sort_key:
42
    | "UPDATED_AT"
43
    | "PUBLISHED_AT"
44
    | "TITLE"
45
    | "AUTHOR"
46
    | "BLOG_TITLE"
47
    | "ID"
48
    | undefined,
49
  reverse: boolean | undefined,
50
  first: number | undefined,
51
  after: string | undefined
52
) {
53
  const filter = [
54
    blog_id ? `blog_id:${blog_id.split("/").pop()}` : "",
55
    query || "",
56
  ]
57
    .filter(Boolean)
58
    .join(" ")
59

60
  const response = await fetch(
61
    `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`,
62
    {
63
      method: "POST",
64
      headers: {
65
        "X-Shopify-Access-Token": auth.token,
66
        "Content-Type": "application/json",
67
        Accept: "application/json",
68
      },
69
      body: JSON.stringify({
70
        query: `query ($first: Int!, $after: String, $query: String, $sortKey: ArticleSortKeys, $reverse: Boolean) { articles(first: $first, after: $after, query: $query, sortKey: $sortKey, reverse: $reverse) { nodes { id title handle isPublished publishedAt author { name } blog { id title } tags summary image { url altText } createdAt updatedAt } pageInfo { hasNextPage endCursor } } }`,
71
        variables: {
72
          first: first || 50,
73
          after: after || undefined,
74
          query: filter || undefined,
75
          sortKey: sort_key || undefined,
76
          reverse: reverse || undefined,
77
        },
78
      }),
79
    }
80
  )
81

82
  if (!response.ok) {
83
    throw new Error(`${response.status} ${await response.text()}`)
84
  }
85

86
  const { data, errors } = await response.json()
87
  if (errors) {
88
    throw new Error(JSON.stringify(errors))
89
  }
90

91
  return data.articles
92
}
93

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
     * Retrieves a list of all articles from a blog
    7
     * Retrieves a list of all articles from a blog. 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
      blog_id: string,
    13
      limit: string | undefined,
    14
      since_id: string | undefined,
    15
      created_at_min: string | undefined,
    16
      created_at_max: string | undefined,
    17
      updated_at_min: string | undefined,
    18
      updated_at_max: string | undefined,
    19
      published_at_min: string | undefined,
    20
      published_at_max: string | undefined,
    21
      published_status: string | undefined,
    22
      handle: string | undefined,
    23
      tag: string | undefined,
    24
      author: string | undefined,
    25
      fields: string | undefined
    26
    ) {
    27
      const url = new URL(
    28
        `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/blogs/${blog_id}/articles.json`
    29
      );
    30
      for (const [k, v] of [
    31
        ["limit", limit],
    32
        ["since_id", since_id],
    33
        ["created_at_min", created_at_min],
    34
        ["created_at_max", created_at_max],
    35
        ["updated_at_min", updated_at_min],
    36
        ["updated_at_max", updated_at_max],
    37
        ["published_at_min", published_at_min],
    38
        ["published_at_max", published_at_max],
    39
        ["published_status", published_status],
    40
        ["handle", handle],
    41
        ["tag", tag],
    42
        ["author", author],
    43
        ["fields", fields],
    44
      ]) {
    45
        if (v !== undefined && v !== "") {
    46
          url.searchParams.append(k, v);
    47
        }
    48
      }
    49
      const response = await fetch(url, {
    50
        method: "GET",
    51
        headers: {
    52
          "X-Shopify-Access-Token": auth.token,
    53
        },
    54
        body: undefined,
    55
      });
    56
      if (!response.ok) {
    57
        const text = await response.text();
    58
        throw new Error(`${response.status} ${text}`);
    59
      }
    60
      return await response.json();
    61
    }
    62