Back

Step by Step Guide to Building a Notion API Integration in PHP

Jul 17, 20246 minute read

Introduction

Hey there, fellow developer! Ready to dive into the world of Notion API integration using PHP? You're in for a treat. We'll be using the awesome mariosimao/notion-sdk-php package to make our lives easier. Buckle up, and let's get started!

Prerequisites

Before we jump in, make sure you've got:

  • A PHP environment up and running
  • Composer installed (trust me, it's a lifesaver)
  • A Notion account and API key (we'll cover how to get this soon)

Got all that? Great! Let's move on.

Installation

First things first, let's get our hands on that mariosimao/notion-sdk-php package. Fire up your terminal and run:

composer require mariosimao/notion-sdk-php

Easy peasy, right?

Setting up the Notion API Integration

Now, let's set up our Notion API integration:

  1. Head over to https://www.notion.so/my-integrations
  2. Click "New integration" and give it a cool name
  3. Grab that API key – we'll need it in a sec

Initializing the Notion Client

Time to write some code! Let's import the necessary classes and create a new Notion client instance:

use Notion\Notion; $notion = Notion::create($_ENV['NOTION_API_KEY']);

Pro tip: Use environment variables to keep your API key safe!

Basic Operations

Retrieving a Page

Let's fetch a page:

$pageId = 'your-page-id-here'; $page = $notion->pages()->find($pageId);

Creating a New Page

Creating a page is a breeze:

$newPage = $notion->pages()->create([ 'parent' => ['page_id' => 'parent-page-id'], 'properties' => [ 'title' => [['text' => ['content' => 'My Awesome New Page']]] ] ]);

Updating a Page

Need to make changes? No problem:

$notion->pages()->update($pageId, [ 'properties' => [ 'title' => [['text' => ['content' => 'Updated Page Title']]] ] ]);

Deleting a Page

Goodbye, page:

$notion->pages()->delete($pageId);

Working with Databases

Retrieving a Database

Fetch that database:

$databaseId = 'your-database-id-here'; $database = $notion->databases()->find($databaseId);

Querying a Database

Let's find some data:

$results = $notion->databases()->query($databaseId, [ 'filter' => [ 'property' => 'Status', 'select' => ['equals' => 'Done'] ] ]);

Adding Items to a Database

Populating your database is simple:

$notion->pages()->create([ 'parent' => ['database_id' => $databaseId], 'properties' => [ 'Name' => [['text' => ['content' => 'New Item']]], 'Status' => ['select' => ['name' => 'In Progress']] ] ]);

Handling Blocks

Retrieving Block Children

Get those child blocks:

$children = $notion->blocks()->children($blockId);

Creating New Blocks

Add some content:

$notion->blocks()->append($pageId, [ ['paragraph' => ['text' => [['text' => ['content' => 'Hello, World!']]]]], ]);

Updating Blocks

Time for an update:

$notion->blocks()->update($blockId, [ 'paragraph' => ['text' => [['text' => ['content' => 'Updated content']]]] ]);

Error Handling and Best Practices

Always wrap your API calls in try-catch blocks:

try { // Your Notion API code here } catch (\Exception $e) { // Handle the error error_log($e->getMessage()); }

And don't forget about rate limits! Be kind to the API.

Conclusion

And there you have it! You're now equipped to build some amazing Notion integrations with PHP. Remember, this is just scratching the surface – there's so much more you can do. Keep exploring, keep coding, and most importantly, have fun!

For more in-depth info, check out the official Notion API docs and the mariosimao/notion-sdk-php documentation.

Now go forth and create something awesome! 🚀