6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

URLパース卒業!kintoneの新APIでゲストスペース判定が一瞬に【APIラボ(開発中の新機能)】

Last updated at Posted at 2025-04-08

はじめに

こんにちは!
サッカー選手から転身してSEをしている@よしきです!

今回は、kintone REST APIで「このアプリがゲストスペースかどうか」を判定し、処理を切り替える必要があるシーンで、つい最近API Labで先行公開された開発中の新API kintone.space.get() を使うと、どれだけコードがシンプルになるかを紹介します。

背景:なぜゲストスペースの判定が必要?

kintoneでは、ゲストスペース内にあるアプリに対してREST APIを使う際、通常とは異なるURLが必要です。

通常の kintone REST API

たとえば、 通常のkintone REST API でゲストスペースのアプリにアクセスする場合は、以下のようなパスを使い、 URL内にguest/{spaceId}を含める必要があります。

  • 通常: /k/v1/record.json
  • ゲストスペース: /k/guest/{spaceId}/v1/record.json

kintone-rest-api-client

一方、 @kintone/rest-api-client(KintoneRestAPIClient) を使う場合には、new KintoneRestAPIClient() の初期化時に guestSpaceId を明示的に渡す必要があります。

const client = new KintoneRestAPIClient({
  auth: { apiToken: 'xxxxx' },
  guestSpaceId: '123' // ← ここが必要!
});

このように、どちらの方法を使うにせよ、 「今自分が操作しているアプリがゲストスペースかどうか」そして「そのスペースIDはいくつか」 という情報が必要になります。

そのため、今操作しているアプリがゲストスペースなのかどうかを判定し、APIの呼び出し方を変える必要があります。

これまでは、この判定を自前で書く必要があり、以下のようなコードが定番でした。

従来の方法(旧コード)

/**
 * ゲストスペース判定 & ゲストスペースID取得
 */
const getGuestSpaceId = () => {
    const url = kintone.api.url("/k/v1/app", true);
    if (url.indexOf("guest") === -1) return null;

    const spaceId = url.split("/").at(5);
    return spaceId;
}

/**
 * レコード一括更新
 */
const guestSpaceId = getGuestSpaceId();
const param = { auth: { apiToken: appCode } };
if (guestSpaceId) param.guestSpaceId = guestSpaceId;

await new KintoneRestAPIClient(param).record
    .updateAllRecords({
        app: kintone.app.getId(),
        records: records,
    })
    .catch(error => {
        throw new Error(error);
    });

このように、URLから無理やりスペースIDを取り出す方法は、パス構造に依存して壊れやすい上に、読みやすさや保守性にも欠けます。

新APIで一気にシンプルに!

注意:この記事で紹介している kintone.space.get() は、2025年4月8日時点では正式リリースされたAPIではなく、「開発中のAPI(API Lab)」として先行公開されている機能です。将来的に仕様が変更されたり、正式サポートされるタイミングで使い方が変わる可能性もありますので、導入の際は十分ご注意ください。

新しいkintone Javascript API kintone.space.get() を使えば、驚くほどスッキリしたコードにできます。

/**
 * レコード一括更新
 */
const spaceInfo = kintone.space.get();
const param = { auth: { apiToken: appCode } };
if (spaceInfo.isGuest) param.guestSpaceId = spaceInfo.id;

await new KintoneRestAPIClient(param).record
    .updateAllRecords({
        app: kintone.app.getId(),
        records: records,
    })
    .catch(error => {
        throw new Error(error);
    });

なんと、たったこれだけ。ゲストスペースかどうか、そしてスペースIDの取得まで1行で完了します!

公式のAPIリファレンスはこちら:

補足:kintone.space.get() は、現在アクセスしている自アプリに対するリクエストにのみ有効です。別のアプリに対してREST APIを使う場合には、そのアプリが属するスペースの情報を別途取得して、guestSpaceId を指定する必要があります。

Before / After 比較

項目 旧API 新API
判定方法 URL解析 kintone.space.get()
コード量 多い 少ない
可読性

まとめ

kintone.space.get() のおかげで、ゲストスペース判定は爆速&確実にできるようになりました。

kintoneカスタマイズをしている方は、今後このAPIを前提に処理を組むことで、よりシンプルかつ安全なコードが書けるはずです。

ぜひ新APIを使ってみましょう!

6
4
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
6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?