0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[Tips]microCMSのマネジメントAPIを利用して、記事のメタ情報を取得する

0
Last updated at Posted at 2025-12-15

先月の記事では microcms-ruby-sdk を使って、簡単なrubyプログラムでmicroCMSから記事データを取得することができました。
2025年10月時点でmicrocms-ruby-sdkが利用しているAPIはコンテンツAPIというのですが、microCMSにはもう一つマネジメントAPIが存在します。
こちらはコンテンツAPIで取得可能な情報に加えて、コンテンツのステータス(公開中・下書き等)や予約投稿している記事の公開開始予定時間など、コンテンツAPIより細かい記事のメタ情報を取得することができます。
今回はこちらを使って記事のより詳細な記事を取得します。

APIトークン作成

記事のメタ情報を取得するためにAPIトークンを取得します。
APIキーを作成する画面でマネジメントAPIのタブを開いてコンテンツの取得にチェックをしましょう。
作成したら後の工程で使うのでトークンを控えておきましょう。

スクリーンショット 2025-10-30 19.04.02.png

利用するGem

マネジメントAPIはベータ機能であるためか、microcms-ruby-sdkはまだ未対応です。そのため今回は環境変数を扱うためのdotenvだけを使います。

Gemfile
# -*- mode: ruby -*-
# frozen_string_literal: true

source 'https://rubygems.org'

gem 'dotenv'

環境変数設定

以下の環境変数を.envファイルに入力していきます。

  • MICROCMS_SERVICE_ID: microCMSのサブドメイン
  • MICROCMS_API_KEY: 利用するAPIキー
  • MICROCMS_ENDPOINT: 利用するAPIのエンドポイント
env
MICROCMS_SERVICE_ID=
MICROCMS_API_KEY=
MICROCMS_ENDPOINT=

実装

記事のメタ情報を取得する関数を作成してみます。
詳細はよくあるAPIの実行プログラムなので割愛しますが、エンドポイントがマネジメントAPIはhttps://(テナントID).microcms-management.io/api/v1/contents/(endpoint)になっているので作成する際は気をつけましょう。

require 'dotenv'

def download_management_contents
  offset = 0
  managements_contents = []
  resource_url = "https://#{ENV('MICROCMS_SERVICE_ID')}.microcms-management.io/api/v1/contents/#{endpoint}"
  uri = URI(resource_url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = (uri.scheme == 'https')
  # SSL周りでエラーになるなら入れる
  # http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  loop do
    params = {limit: LIMIT, offset: offset}
    uri.query = URI.encode_www_form(params)
    req = Net::HTTP::Get.new(uri.request_uri)
    req['X-MICROCMS-API-KEY'] = ENV('MICROCMS_API_KEY')
    res = http.request(req)
    items = JSON.parse(res.body)
    puts "fetching management contents... #{managements_contents.size}/#{items['totalCount']}"
    managements_contents += items['contents']
    break if items['contents'].size < LIMIT
    sleep 1
    
    offset += LIMIT
  end
  managements_contents
end

実際に実行してみると以下のような結果が得られます(一部カラムを匿名化しています)。
コンテンツIDと比べて記事の状態(公開しているかと下書き保有しているか)や、記事を誰が作成したかがわかるようになっています。

[
  {
    "id": "xxxx",
    "status": ["PUBLISH","DRAFT"],
    "createdAt": "2025-10-28T07:44:08.494Z",
    "updatedAt": "2025-10-28T07:48:23.988Z",
    "createdBy": "xxxx",
    "updatedBy": "xxxx",
    "draftKey": "xxxx"
  },
  {
    "id": "xxxx",
    "status": ["PUBLISH","DRAFT"],
    "createdAt": "2025-10-18T23:49:50.903Z",
    "updatedAt": "2025-10-30T03:14:49.476Z",
    "createdBy": "xxxx",
    "updatedBy": "xxxx",
    "draftKey": "xxxx"
  },
  {
    "id": "xxxx",
    "status": ["DRAFT"],
    "createdAt": "2025-10-17T07:15:37.731Z",
    "updatedAt": "2025-10-17T07:17:14.568Z",
    "createdBy": "xxxx",
    "updatedBy": "xxxx",
    "draftKey": "xxxx"
  },
  // 後略
]

コンテンツAPIとマネジメントAPIは取れる情報が違うため、ほしい情報によって使い分けたり組み合わせることでmicroCMSをより効果的に活用することができます。
microcms-ruby-sdk での対応がくることを待ちつつ、しばらくは直接APIを叩いて利用しようと思います。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?