Skip to main content

Notion

Need help deploying these sources, or figuring out how to run them in your data stack?

Join our Slack community or book a call with our support engineer Adrian.

Notion is a flexible workspace tool for organizing personal and professional tasks, offering customizable notes, documents, databases, and more.

This Notion dlt verified source and pipeline example loads data using “Notion API” to the destination of your choice.

Sources that can be loaded using this verified source are:

NameDescription
notion_databasesRetrieves data from Notion databases.

Setup Guide

Grab credentials

  1. If you don't already have a Notion account, please create one.
  2. Access your Notion account and navigate to My Integrations.
  3. Click "New Integration" on the left and name it appropriately.
  4. Finally, click on "Submit" located at the bottom of the page.

Add a connection to the database

  1. Open the database that you want to load to the destination.

  2. Click on the three dots located in the top right corner and choose "Add connections".

    Notion Database

  3. From the list of options, select the integration you previously created and click on "Confirm".

Note: The Notion UI, which is described here, might change. The full guide is available at this link.

Initialize the verified source

To get started with your data pipeline, follow these steps:

  1. Enter the following command:

    dlt init notion duckdb

    This command will initialize the pipeline example with Notion as the source and duckdb as the destination.

  2. If you'd like to use a different destination, simply replace duckdb with the name of your preferred destination.

  3. After running this command, a new directory will be created with the necessary files and configuration settings to get started.

For more information, read the Walkthrough: Add a verified source.

Add credentials

  1. In the .dlt folder, there's a file called secrets.toml. It's where you store sensitive information securely, like access tokens. Keep this file safe. Here's its format for service account authentication:

    # Put your secret values and credentials here
    # Note: Do not share this file and do not push it to GitHub!
    [source.notion]
    api_key = "set me up!" # Notion API token (e.g. secret_XXX...)
  2. Replace the value of api_key with the one that you copied above. This will ensure that your data-verified source can access your Notion resources securely.

  3. Next, follow the instructions in Destinations to add credentials for your chosen destination. This will ensure that your data is properly routed to its final destination.

For more information, read the General Usage: Credentials.

Run the pipeline

  1. Before running the pipeline, ensure that you have installed all the necessary dependencies by running the command:
    pip install -r requirements.txt
  2. You're now ready to run the pipeline! To get started, run the following command:
    python notion_pipeline.py
  3. Once the pipeline has finished running, you can verify that everything loaded correctly by using the following command:
    dlt pipeline <pipeline_name> show
    For example, the pipeline_name for the above pipeline example is notion, you may also use any custom name instead.

For more information, read the Walkthrough: Run a pipeline.

Sources and resources

dlt works on the principle of sources and resources.

Source notion_databases

This function loads notion databases from notion into the destination.

@dlt.source
def notion_databases(
database_ids: Optional[List[Dict[str, str]]] = None,
api_key: str = dlt.secrets.value,
) -> Iterator[DltResource]:

database_ids: A list of dictionaries each containing a database id and a name.

api_key: The Notion API secret key.

If "database_ids" is None, the source fetches data from all integrated databases in your Notion account.

It is important to note that the data is loaded in “replace” mode where the existing data is completely replaced.

Customization

Create your own pipeline

If you wish to create your own pipelines, you can leverage source and resource methods from this verified source.

  1. Configure the pipeline by specifying the pipeline name, destination, and dataset as follows:

    pipeline = dlt.pipeline(
    pipeline_name="notion", # Use a custom name if desired
    destination="duckdb", # Choose the appropriate destination (e.g., duckdb, redshift, post)
    dataset_name="notion_database" # Use a custom name if desired
    )

    To read more about pipeline configuration, please refer to our documentation.

  2. To load all the integrated databases:

    load_data = notion_databases()
    load_info = pipeline.run(load_data)
    print(load_info)
  3. To load the custom databases:

    selected_database_ids = [{"id": "0517dae9409845cba7d","use_name":"db_one"}, {"id": "d8ee2d159ac34cfc"}]
    load_data = notion_databases(database_ids=selected_database_ids)
    load_info = pipeline.run(load_data)
    print(load_info)

    The Database ID can be retrieved from the URL. For example if the URL is:

    https://www.notion.so/d8ee2d159ac34cfc85827ba5a0a8ae71?v=c714dec3742440cc91a8c38914f83b6b

    The database ID in the given Notion URL is: "d8ee2d159ac34cfc85827ba5a0a8ae71".

The database ID in a Notion URL is the string right after notion.so/, before any question marks. It uniquely identifies a specific page or database.

The database name ("use_name") is optional; if skipped, the pipeline will fetch it from Notion automatically.

This demo works on codespaces. Codespaces is a development environment available for free to anyone with a Github account. You'll be asked to fork the demo repository and from there the README guides you with further steps.
The demo uses the Continue VSCode extension.

Off to codespaces!

DHelp

Ask a question

Welcome to "Codex Central", your next-gen help center, driven by OpenAI's GPT-4 model. It's more than just a forum or a FAQ hub – it's a dynamic knowledge base where coders can find AI-assisted solutions to their pressing problems. With GPT-4's powerful comprehension and predictive abilities, Codex Central provides instantaneous issue resolution, insightful debugging, and personalized guidance. Get your code running smoothly with the unparalleled support at Codex Central - coding help reimagined with AI prowess.