I am using a serverless function deployed on Netlify to fetch data from a Notion database with an Internal Integration Token and trying to fetch the data with:
const promise = fetch('<deployed website url on netlify>/.netlify/functions/notion-proxy');
promise.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));
The serverless function is reached because I get my custom error in the catch statement ("Internal server error") and an error message in the body: "Make sure the relevant pages and databases are shared with your integration" - even though I have already shared the database with the integration. I have checked that the integration has the appropriate permissions to access the database, and I have also tried sharing the database with the integration again, but the error still persists.
Here's the serverless function's code:
import { Client } from "@notionhq/client";
const apiKey = process.env.API_KEY;
const databaseId = process.env.DATABASE_ID;
const notion = new Client({ auth: apiKey });
const username = ""; // Notion username
const handler = async (event) => {
try {
const response = await notion.databases.query({
database_id: databaseId,
filter: {
property: "ID",
rich_text: {
contains: username,
},
},
});
return {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE",
},
body: JSON.stringify(response),
};
} catch (error) {
console.log("error.body: ", error.body);
return {
statusCode: 500,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE",
},
body: JSON.stringify({
error: "Internal server error",
errorBody: error.body
}),
};
}
};
module.exports = { handler };
What else can I do to solve this issue?
I've updated the code and now the serverless function is reached but I still can't fetch the data from the notion database - the error asks me to check the permissions for the integration although I already added all of the permissions (later on I want to query the fetched data on Webflow)