LoginSignup
2
1

More than 1 year has passed since last update.

DenoでNotionのAPIを触ってみる

Last updated at Posted at 2022-12-21

DenoでもNotionのAPI触ってみたいなーと調べたら、Node.jsのSDKと同じような使い勝手で利用できる非公式SDKがあったので触ってみました。

環境

$ deno --version
deno 1.29.1 (release, x86_64-apple-darwin)
v8 10.9.194.5
typescript 4.9.4

notion_sdk_deno

執筆次点だと0.3.9が最新の模様です。

app.ts
import { Notion } from "https://deno.land/x/notion_sdk_deno@v0.3.9/mod.ts";

const notion = new Notion({
    token: "APIトークン",
  });

const userList = await notion.getUsers();
console.log("userList = ", userList);

実行には--allow-net--allow-envのフラグが必要です。

$deno run --allow-net --allow-env app.ts

urlSearchParams =  URLSearchParams {
  [Symbol(list)]: [],
  [Symbol("url object")]: null,
  [Symbol("[[webidl.brand]]")]: Symbol("[[webidl.brand]]")
}
userList =  [
  {
    object: "user",
    id: "xxxxxxxxxxxxxxxxxxxxxx",
    name: "のびすけ",

省略

 },
  {
    object: "user",
    id: "23feb307-xxxxxxxxxxx",
    name: "ほげほげ",
    avatar_url: null,
    type: "bot",
    bot: { owner: { type: "workspace", workspace: true }, workspace_name: "Hogehoge" }
  }

サンプル通りで動いてよかった

notion_sdk

サンプルを動かす

app.ts
import { Client } from "https://deno.land/x/notion_sdk/src/mod.ts";

// Initializing a client
const notion = new Client({
  auth: 'APIトークン',
})

const listUsersResponse = await notion.users.list({})

console.log(listUsersResponse)

実行は先ほど同様

少しハマった

サンプルページのサンプルだと↓の書き方になっていてこちらだと、Deno.env.get()を使ってNOTION_TOKENの環境変数を取得するコードになってました。 Node.jsでprocess.envを使うイメージですね

app.ts
import { Client } from "https://deno.land/x/notion_sdk/src/mod.ts";

// Initializing a client
const notion = new Client({
  auth: Deno.env.get("NOTION_TOKEN"),
})

const listUsersResponse = await notion.users.list({})

console.log(listUsersResponse)

こんな感じで実行すればOKです。

$ NOTION_TOKEN=APIトークン deno run --allow-net --allow-env app.ts

どっちを使うか

このくらいの利用だとどちらでも良さそうって感じでしたが、なんとなく最終更新が比較的最近。v1としている。の2点から

こっちの方がいいのかも?くらいな印象です。

2
1
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
2
1