内容

新しい search endpoint を使って、ページやデータベースが検索できるようになった。

以前は、 list databases endpoint を使っていたけど、こっちの方が機能的だと言っている。

テスト

このページ自身を検索してみるとこんな感じ。NOTION_API_KEY はこのテスト用に作ったキーで環境変数に登録してある。リンク先にあるコマンドそのまま(検索する文字列と改行位置だけ変更した)。

#!/bin/sh
curl -X POST '<https://api.notion.com/v1/search>' \\
  -H 'Authorization: Bearer '"$NOTION_API_KEY"'' \\
	-H 'Content-Type: application/json' \\
  -H "Notion-Version: 2021-08-16" \\
	--data '{
    "query":"Search",
    "sort":{
      "direction":"ascending",
      "timestamp":"last_edited_time"
    }
  }'

実行すると json が返ってくる。読みにくいので vscode でインデント整理したものを示した。result の中に結果の配列が入ってきているのがわかる。今回は1件だけ検出された。他に Search という言葉は使っていなかったようで、1件しか検出されなかった。

{
    "object": "list",
    "results": [
        {
            "object": "page",
            "id": "53f9fefc-ed3e-4cd9-9591-f6083033ae22",
            "created_time": "2021-11-08T21:46:00.000Z",
            "last_edited_time": "2021-11-08T23:14:00.000Z",
            "cover": null,
            "icon": null,
            "parent": {
                "type": "database_id",
                "database_id": "c63c00ad-201d-4bed-82b0-18dfca9b6ba0"
            },
            "archived": false,
            "properties": {
                "日付": {
                    "id": "%3CWbe",
                    "type": "date",
                    "date": {
                        "start": "2021-05-05",
                        "end": null
                    }
                },
                "原題": {
                    "id": "%3FMR%7D",
                    "type": "rich_text",
                    "rich_text": [
                        {
                            "type": "text",
                            "text": {
                                "content": "Search is now available in the API",
                                "link": null
                            },
                            "annotations": {
                                "bold": false,
                                "italic": false,
                                "strikethrough": false,
                                "underline": false,
                                "code": false,
                                "color": "default"
                            },
                            "plain_text": "Search is now available in the API",
                            "href": null
                        }
                    ]
                },
                "URL": {
                    "id": "o%3A~F",
                    "type": "url",
                    "url": "<https://developers.notion.com/changelog/search-is-now-available-in-the-api>"
                },
                "タグ": {
                    "id": "tUJV",
                    "type": "multi_select",
                    "multi_select": [
                        {
                            "id": "fcb7bc23-a43d-4ff1-967f-db59a6125492",
                            "name": "ADDED",
                            "color": "green"
                        }
                    ]
                },
                "名前": {
                    "id": "title",
                    "type": "title",
                    "title": [
                        {
                            "type": "text",
                            "text": {
                                "content": "API で Search が使えるようになりました",
                                "link": null
                            },
                            "annotations": {
                                "bold": false,
                                "italic": false,
                                "strikethrough": false,
                                "underline": false,
                                "code": false,
                                "color": "default"
                            },
                            "plain_text": "API で Search が使えるようになりました",
                            "href": null
                        }
                    ]
                }
            },
            "url": "<https://www.notion.so/API-Search-53f9fefced3e4cd99591f6083033ae22>"
        }
    ],
    "next_cursor": null,
    "has_more": false
}

Notion API Changelog まとめ