Your first request

Make your first API call.

2 min read

Congratulations on installing the SDK and setting up your authentication!

You're now ready to make your first request to the Notion API. This guide will walk you through a simple example: listing the users in your workspace.

Goal: Listing users

Our goal is to fetch a list of all users associated with your integration token and print their names to the console. This is a great way to verify that your connection to the API is working correctly.

Code

Here is the complete PHP script to achieve this. You can save it as a file (e.g., ) and run it from your terminal.

<?php

require_once 'vendor/autoload.php';

use Brd6\NotionSdkPhp\Client;
use Brd6\NotionSdkPhp\ClientOptions;
use Brd6\NotionSdkPhp\Resource\Pagination\UserResults;
use Brd6\NotionSdkPhp\Resource\User\AbstractUser;

// Use an environment variable for your token
$token = getenv('NOTION_TOKEN');

if (!$token) {
    die('Error: NOTION_TOKEN environment variable not set.' . PHP_EOL);
}

// Initialize the client
$options = (new ClientOptions())->setAuth($token);
$notion = new Client($options);

echo "Fetching users..." . PHP_EOL;

/** @var UserResults $users */
$users = $notion->users()->list();

if (count($users->getResults()) === 0) {
    echo "No users found." . PHP_EOL;
    exit;
}

echo "Found " . count($users->getResults()) . " users:" . PHP_EOL;

/** @var AbstractUser $user */
foreach ($users->getResults() as $user) {
    echo "- User ID: " . $user->getId() . PHP_EOL;
    echo "  Name: " . $user->getName() . PHP_EOL;
    echo "  Type: " . $user->getType() . PHP_EOL;
}

Next steps

You've successfully made your first API request! You've learned how to initialize the client, call an endpoint, and process a paginated list of resources.

To deepen your understanding of the SDK's architecture, we recommend exploring the Core Concepts section, starting with an overview of the Client

Did this answer your question?