// Define the Notion resource type as required for the function
type Notion = {
token: string
}
export async function main(notion: Notion, databaseId: string): Promise<any> {
// The URL for fetching data from a Notion database should be replaced with the actual database URL
const databaseUrl = `https://api.notion.com/v1/databases/${databaseId}/query`;
// Set up the request headers
const headers = {
'Authorization': `Bearer ${notion.token}`,
'Notion-Version': '2022-06-28', // Use the latest version supported by your integration
'Content-Type': 'application/json'
};
// Perform the fetch request to get data from the Notion database
try {
const response = await fetch(databaseUrl, {
method: 'POST', // The Notion API requires a POST request to query a database
headers: headers,
body: JSON.stringify({}) // You can customize the body according to the Notion API documentation for filtering, sorting, etc.
});
// Check if the request was successful
if (!response.ok) {
throw new Error(`Failed to fetch Notion database: ${response.status} ${response.statusText}`);
}
// Parse the JSON response
const data = await response.json();
// Return the parsed data
return data;
} catch (error) {
// Handle any errors that occurred during the fetch
console.error('Error fetching Notion database:', error);
throw error; // Rethrow the error to be handled by the caller
}
}
Submitted by henri186 250 days ago
// Define the Notion resource type as required for the function
type Notion = {
token: string
}
export async function main(notion: Notion): Promise<any> {
// The URL for fetching data from a Notion database should be replaced with the actual database URL
const databaseUrl = 'https://api.notion.com/v1/databases/613948ef64954ac2bd2515ca3fc71c1e/query';
// Set up the request headers
const headers = {
'Authorization': `Bearer ${notion.token}`,
'Notion-Version': '2022-06-28', // Use the latest version supported by your integration
'Content-Type': 'application/json'
};
// Perform the fetch request to get data from the Notion database
try {
const response = await fetch(databaseUrl, {
method: 'POST', // The Notion API requires a POST request to query a database
headers: headers,
body: JSON.stringify({}) // You can customize the body according to the Notion API documentation for filtering, sorting, etc.
});
// Check if the request was successful
if (!response.ok) {
throw new Error(`Failed to fetch Notion database: ${response.status} ${response.statusText}`);
}
// Parse the JSON response
const data = await response.json();
// Return the parsed data
return data;
} catch (error) {
// Handle any errors that occurred during the fetch
console.error('Error fetching Notion database:', error);
throw error; // Rethrow the error to be handled by the caller
}
}
Submitted by henri186 250 days ago