Hey, friends today I will teach you How to use the Notion API with Google Apps Script to connect Gmail, Google Forms, and Google Sheets with your Notion workspace. so let get started with today Code snippets. Getting different problems is altogether gives a very different experience. today the Code snippets I am going to share with you is How to use the Notion API with Google Apps Script to connect Gmail, Google Forms, and Google Sheets with your Notion workspace.
The notion, my all-time favourite tool for storing everything from web pages to code snippets to recipes, has just gotten better. They’ve released a public API, making it much easier for developers to read and write to their Notion workspace from outside apps.
You can, for example, create a document in Google Docs and export it to Notion while still in Docs. Users of Google Sheets can import pages from the Notion database into their spreadsheet. Any new Google Forms submissions can be saved directly to Notion, and so on!
You might also like our trending code snippets
- How to Delete Blank Rows from Tables in your Google Documents
- How to Convert HTML to PDF with Google Script
- How to Request Payments with Razorpay and Google Sheets
- Create a Telegram Bot for Sending Notifications using Google Apps Script
Save Gmail Messages in Notion
I have put together a Gmail add-on that makes it easy for you to save email messages, or any other text content, from Gmail to your Notion workspace with a click. Here’s how the app works.
Step 1: Connect Gmail to Notion
Step 2:Allow Access to Notion Pages – If you have multiple databases in your Notion workspace, you can grant access to only a subset of them, leaving the rest inaccessible to the external app..
Step 3: Choose Email – open any email message in Gmail and you’ll be able to edit the subject and body of the email before sending it to your Notion page. Please keep in mind that the app currently only supports plain text format.
Step 4: Open Notion – When you click the Send to Notion button, the contents of the currently selected email message are saved in your Notion database. To view a recently added page, click the All updates link in your Notion sidebar.
How to Use Notion with Google Apps Script
If you would to integrate your own Google add-on with Notion API, here’s a brief outline of the steps involved.
- Go to notion.so and click the
Create New Integration
button. You’ll be provided with a Client ID and Client Secret that you’ll need in a later step. - Include the OAuth2 library in your Apps Script project and invoke the
getRedirectUri
method to get the OAuth2 redirect URL for the previous step.
const getNotionService = () => {
return OAuth2.createService("Notion")
.setAuthorizationBaseUrl("https://api.notion.com/v1/oauth/authorize")
.setTokenUrl("https://api.notion.com/v1/oauth/token")
.setClientId(CLIENT_ID)
.setClientSecret(CLIENT_SECRET)
.setCallbackFunction("authCallback")
.setPropertyStore(PropertiesService.getUserProperties())
.setCache(CacheService.getUserCache())
.setTokenHeaders({
Authorization: `Basic ${Utilities.base64Encode(
`${CLIENT_ID}:${CLIENT_SECRET}`
)}`,
});
};
const authCallback = (request) => {
const isAuthorized = getNotionService().handleCallback(request);
return HtmlService.createHtmlOutput(
isAuthorized ? "Success!" : "Access Denied!"
);
};
const getRedirectUri = () => {
console.log(OAuth2.getRedirectUri());
};
- Connect to Notion API – Make a
Get
HTTP request to the /vi/databases to fetch a list of all databases that the user has explicitly shared with authorized app.
function getDatabasesList() {
var service = getNotionService();
if (service.hasAccess()) {
const url = "https://api.notion.com/v1/databases";
const response = UrlFetchApp.fetch(url, {
headers: {
Authorization: `Bearer ${service.getAccessToken()}`,
"Notion-Version": "2021-05-13",
},
});
const { results = [] } = JSON.parse(response.getContentText());
const databases = results
.filter(({ object }) => object === "database")
.map(({ id, title: [{ plain_text: title }] }) => ({ id, title }));
console.log({ databases });
} else {
console.log("Please authorize access to Notion");
console.log(service.getAuthorizationUrl());
}
}
Download Gmail to Notion
The Gmail to Notion app is in beta. If you would like to use it with your Gmail or Google Workspace account, please install from here – Gmail to Notion