Assigning Notion Page Access via API

I’m trying to figure out how to give a specific user access to a Notion page using their API. I’ve looked at different endpoints like update, but nothing seems to work. Here’s a PHP function I tried:

function setUserPageAccess($email, $pageId) {
    $apiUrl = 'https://api.notion.com/v1/pages/' . $pageId . '/permissions';
    $headers = [
        'Authorization: Bearer YOUR_TOKEN_HERE',
        'Content-Type: application/json',
        'Notion-Version: 2022-06-28'
    ];
    $payload = [
        'role' => 'editor',
        'user_id' => $email
    ];

    $curl = curl_init($apiUrl);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($payload));

    $result = curl_exec($curl);
    curl_close($curl);

    if ($result) {
        echo 'Access granted to ' . $email;
    } else {
        echo 'Failed to grant access to ' . $email;
    }
}

Has anyone successfully done this? What am I missing?

hey mikezhang, i’ve had similar issues. the api can be tricky. have u tried using the ‘invite’ endpoint instead? it’s like /v1/pages/{page_id}/invite. also, make sure ur using the user’s actual notion id, not just their email. that tripped me up at first. good luck!

I’ve worked extensively with the Notion API, and I can confirm that granting page access can be a bit tricky.

One key point that hasn’t been mentioned yet is the importance of workspace permissions. Make sure the integration you’re using has the appropriate permissions in the workspace where the page is located.

Also, it’s worth noting that you might need to use the ‘search’ endpoint first to find the user’s ID if you only have their email. The API typically requires user IDs rather than email addresses for most operations.

Another tip: always check the API response codes and messages. Notion’s API is pretty good at providing descriptive error messages that can help pinpoint issues. If you’re still stuck, Notion’s developer forum is a great resource for troubleshooting specific API problems.

I’ve encountered this issue before, and the solution lies in using the correct endpoint and payload structure. The ‘/permissions’ endpoint you’re using is actually for retrieving permissions, not setting them. For granting access, you should use the ‘/v1/pages/{page_id}/users’ endpoint with a POST request.

Here’s a revised version of your function that should work:

function setUserPageAccess($email, $pageId) {
    $apiUrl = 'https://api.notion.com/v1/pages/' . $pageId . '/users';
    $headers = [
        'Authorization: Bearer YOUR_TOKEN_HERE',
        'Content-Type: application/json',
        'Notion-Version: 2022-06-28'
    ];
    $payload = [
        'add_users' => [
            [
                'email' => $email,
                'role' => 'editor'
            ]
        ]
    ];

    // Rest of your cURL setup remains the same

    // Process the result
    $response = json_decode($result, true);
    if (isset($response['results'])) {
        echo 'Access granted to ' . $email;
    } else {
        echo 'Failed to grant access to ' . $email;
    }
}

This should resolve your issue. Remember to handle potential errors and edge cases in a production environment.