Notion Ruby SDK

ci Gem Version

Unofficial Ruby client for the Notion APIs.

Installation

Add this line to your application's Gemfile:

gem 'notion-sdk-ruby'

And then execute:

$ bundle install

Usage

Initialize Notion::Client with your app's integration secret.

require "notion-sdk-ruby"
client = Notion::Client.new(token: ENV["NOTION_API_SECRET"])

API reference

Databases

databases#retrieve

API reference

client.databases.retrieve("668d797c-76fa-4934-9b05-ad288df2d136")

databases#list

API reference

client.databases.list

databases#query

API reference

client.databases.query("668d797c-76fa-4934-9b05-ad288df2d136", {
  filter: {
    or: [
      {
        property: "In stock",
        checkbox: {
          equals: true
        }
      },
      {
        property: "Cost of next trip",
        number: {
          greater_than_or_equal_to: 2
        }
      }
    ]
  },
  sorts: [
    {
      property: "Last ordered",
      direction: "ascending"
    }
  ]
})

databases#create

API reference

client.databases.create({
  parent: {
    type: "page_id",
    page_id: "98ad959b-2b6a-4774-80ee-00246fb0ea9b"
  },
  title: [
    {
        type: "text",
        text: {
            content: "Grocery List",
            link: null
        }
    }
  ],
  properties: {
    Name: {
      title: {}
    }
  }
})

databases#update

API reference

client.databases.update("668d797c-76fa-4934-9b05-ad288df2d136", {
  title: [
    {
        type: "text",
        text: {
            content: "Grocery List",
            link: null
        }
    }
  ],
  properties: {
    Name: {
      title: {}
    }
  }
})

Pages

pages#retrieve

API reference

client.pages.retrieve("b55c9c91-384d-452b-81db-d1ef79372b75")

pages#create

API reference

client.pages.create({
  parent: { database_id: "48f8fee9cd794180bc2fec0398253067" },
  properties: {
    Name: {
      title: [
        {
          text: {
            content: "Tuscan Kale"
          }
        }
      ]
    },
    Description: {
      rich_text: [
        {
          text: {
            content: "A dark green leafy vegetable"
          }
        }
      ]
    },
    Food group: {
      select: {
        name: "Vegetable"
      }
    },
    Price: { number: 2.5 }
  },
  children: []
})

pages#update

API reference

client.pages.update("b55c9c91-384d-452b-81db-d1ef79372b75", {
  properties: {
    "In stock": { checkbox: true }
  }
})

Blocks

blocks#retrieve

API reference

client.blocks.retrieve("b55c9c91-384d-452b-81db-d1ef79372b75")

blocks#update

API reference

client.blocks.update("b55c9c91-384d-452b-81db-d1ef79372b75", {
  to_do: {
    text: [{ 
      text: { content: "Lacinato kale" } 
    }],
    checked: false
  }
})

blocks#children#list

API reference

client.blocks.children.list("b55c9c91-384d-452b-81db-d1ef79372b75", {
  page_size: 100
})

blocks#children#append

API reference

client.blocks.children.append("b54c9c91-384d-452b-81db-d1ef79372b75", {
  children: [
    {
      object: "block",
      type: "heading_1",
      heading_1: {
        text: [{ type: "text", text: { content: "Lacinato kale" } }]
      }
    },
    {
      object: "block",
      type: "paragraph",
      paragraph: {
        text: [
          {
            type: "text",
            text: {
              content: "Lacinato kale is a variety of kale with a long tradition in Italian cuisine, especially that of Tuscany. It is also known as Tuscan kale, Italian kale, dinosaur kale, kale, flat back kale, palm tree kale, or black Tuscan palm.",
              link: { url: "https://en.wikipedia.org/wiki/Lacinato_kale" }
            }
          }
        ]
      }
    }
  ]
})

Users

users#retrieve

API reference

client.users.retrieve("d40e767c-d7af-4b18-a86d-55c61f1e39a4")

users#list

API reference

client.users.list

API reference

client.search({
  query: "External tasks",
  sort: {
    direction: "ascending",
    timestamp: "last_edited_time"
  }
})

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

Before using bin/console you need to create a new file, .env, at the root project directory. This will enable you to run commands directly against your Notion integration.

cat > .env <<EOF
API_SECRET=<YOUR NOTION API SECRET HERE>
EOF

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/mgmarlow/notion-sdk-ruby.

License

The gem is available as open source under the terms of the MIT License.