Connector Object

Classes:

Connector(api_key, **kwargs)

Notion API Client

class Connector(api_key, **kwargs)[source]

Bases: object

Notion API Client

This allows interacting with the Notion API. You can use this to create databases, add rows to databases, and query databases. For any other API calls that are not implemented, you can use the request method.

For more, see reference at: https://developers.notion.com/reference/intro

All the methods in this class allow you to pass in additional arguments to the request body. These arguments will override the arguments that are passed in as parameters to the method if they use the same keys.

Parameters:
  • api_key (str) – The API key

  • **kwargs – Additional arguments to pass to the requests session

Example

>>> import notion
>>> client = notion.Connector("API_KEY")
>>> client.query_db("DATABASE_ID")

Methods:

query_db(db_id[, filter, sorts])

Query a database as per the notion API specifcation.

create_db(schema, parent, title[, cover_url])

Creates a database as per the notion API specification.

update_db(db_id, schema[, title])

Updates a database schema as per the notion API specification.

get_db_schema(db_id[, json])

Gets the schema of a database

add_row_to_db(db_id, row, **kwargs)

Adds a DatabaseRow object to a database

request(method, url, **kwargs)

Makes a custom request to the notion API

query_db(db_id: str, filter: Optional[Dict] = None, sorts: Optional[List[Dict]] = None, **kwargs) Dict[source]

Query a database as per the notion API specifcation. Note: Corresponds to the POST /databases/{database_id}/query endpoint.

Parameters:
  • db_id (str) – The database id

  • filter (Optional[Dict]) – The filter to apply to the query (see notion API docs)

  • sorts (Optional[List[Dict]]) – The sorts to apply to the query (see notion API docs)

  • **kwargs – Additional arguments to pass to the POST request body (These may override the filter and sorts arguments if they use the same keys)

Returns:

The response from the API

Return type:

Dict

create_db(schema: Schema, parent: str, title: str, cover_url: Optional[str] = None, **kwargs) Dict[source]

Creates a database as per the notion API specification. Uses a schema object to create the columns and properties of the database. To add rows to the database use the add_row_to_db method after creating the database.

Note: Corresponds to the POST /databases/{database_id}/ endpoint.

Parameters:
  • schema (Schema) – The schema of the database

  • parent (str) – The parent ID of the database

  • title (str) – The title of the database

  • cover_url (Optional[str]) – The cover url of the database

  • **kwargs – Additional arguments to pass to the POST request body (These may override the schema, parent, title, and cover_url arguments if using the same keys)

Returns:

The response from the API

Return type:

Dict

update_db(db_id: str, schema: Schema, title=None) Dict[source]

Updates a database schema as per the notion API specification. Uses a schema object to update the columns and properties of the database.

Note: Corresponds to the PATCH /databases/{database_id} endpoint.

Parameters:
  • db_id (str) – database ID

  • schema (Schema) – schema object to update

  • title (str, optional) – title to change. Defaults to None.

  • **kwargs – Additional arguments to pass to the PATCH request body (These may override the schema, parent, title, and cover_url arguments if using the same keys)

Returns:

response from the API

Return type:

Dict

Example

>>> import notion
>>> client = notion.Connector("API_KEY")
>>> schema = notion.Schema(TITLE = notion.Title("Name"), AGE = notion.Number("Age"))
>>> client.update_db("DATABASE_ID", schema)
get_db_schema(db_id: str, json=False) Schema[source]

Gets the schema of a database

Note: Corresponds to the GET /databases/{database_id} endpoint.

Parameters:
  • db_id (str) – Database ID

  • json (bool, optional) – Set to True to return the raw json instead of Schema obj. Defaults to False.

Returns:

The schema object of the database (or json if json is True)

Return type:

Schema

add_row_to_db(db_id: str, row, **kwargs) Dict[source]

Adds a DatabaseRow object to a database

Note: Corresponds to the POST /pages endpoint.

Parameters:
  • db_id (str) – Database ID

  • row (DatabaseRow|Dict|str) – DatabaseRow object or JSON object based on API specification

  • **kwargs – Additional arguments to pass to the POST

Returns:

response from the API

Return type:

Dict

request(method, url, **kwargs)[source]

Makes a custom request to the notion API

Uses underlying requests.Session object to make a request to the notion API. See the requests documentation for more information.

Parameters:
  • method (str) – type of request, eg. GET, POST, PATCH, etc.

  • url (str) – url to make the request to (must be a notion API url or extension)

Returns:

response

Return type:

Dict