APIの実行
APIについて
RunbookではGraphQL形式のAPIが提供されています。
エンドポイント
https://<サブドメイン>.runbook.jp/api/graphqlメソッド
POST
認証
Authorizationヘッダに、"Bearer "に続いてAPIトークン、またはOAuthのアクセストークンをセットします。
例
Authorization: Bearer <APIトークン>APIトークンの取得方法はこちらを参照してください。
実行サンプル
Node.jsによる実行サンプルです。
getArticlesQuery.mjs
export default `
query getArticles($bookUid: ID!, $first: Int, $offset: Int) {
node(id: $bookUid) {
... on Book {
__typename
name
articles(
first: $first
offset: $offset
) {
totalCount
nodes {
uid
name
slug
id
bodyText
createdAt
updatedAt
}
}
}
}
}
`;index.mjs
// クエリ文字列
import getArticleQuery from "./getArticlesQuery.mjs";
if (process.argv.length < 5) {
console.error("Usage: node index.js <host> <token> <bookUid>");
process.exit(1);
}
// Runbookのドメイン(例: example.runbook.jp)
const host = process.argv[2];
// RunbookのAPIトークン
const token = process.argv[3];
// BookのUID
const bookUid = process.argv[4];
async function postData(url, token, data) {
try {
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify(data),
});
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const result = await response.json();
// 結果を出力
console.log(JSON.stringify(result));
} catch (error) {
console.error("Error posting data:", error.message);
}
}
export function getEndpoint(host) {
return `https://${host}/api/graphql`;
}
postData(getEndpoint(host), token, {
query: getArticleQuery,
variables: { bookUid, first: 50, offset: 0 },
});実行例
node index.mjs <サブドメイン>.runbook.jp <APIトークン> <ブックのUID>SDK
Node.js
最終更新: