Action

Add To Notion

Posted by moggy1972, Last update 3 days ago

UPDATES

3 days ago

Tidied up and added more notes to make scripts easier to follow.

show all updates...

3 days ago

Tidied up and added more notes to make scripts easier to follow.

3 days ago

Update to include version number in all functions (now required by Notion API)

27 days ago

Updated to include version number.

Action that adds current draft to Notion in the page you use as your inbox. If it’s a single line, it’s added as a note, if it’s multiple lines, it’s a page. Currently text is added as one block and it doesn’t convert markdown.

Prompts for your Notion Secret key plus inbox location on first run.

Steps

  • script

    // Variables and Functions for Notion
    // These just set up some functions that can then be called in subsequent scripts. 
    
    // Basic variables - sets up fixed variables to be used in all the functions below
    base = "https://api.notion.com/v1/"
    version = "2021-05-13"
    
    //// Secret token and inbox
    // inbox is the page id of the page where you want to save to by default.
    var credential = Credential.create("Notion", "Notion API");
    credential.addPasswordField("secret", "Secret");
    credential.addTextField("inbox", "Inbox");
    credential.authorize();
    var secret = credential.getValue("secret")
    var inbox = credential.getValue("inbox") 
    
    // Functions
    
    function notionClient() {
    	var credential = Credential.create("Notion", "Notion API");
    	credential.addPasswordField("secret", "Secret");
    	credential.authorize();
    
    	//Get token
    	var secret = credential.getValue("secret")
    
    	return secret
    }
    
    function getUsers() {
    	url = base+"users"
    	var http = HTTP.create(); // create HTTP object
    
    	var response = http.request({
      		"url": url,
      		"method": "GET",
      		"headers": {
        		"Authorization": "Bearer " + secret,
        		"Notion-Version": version
      		}
    	});
    
    	if (response.success) {
      		var text = response.responseText;
      		var data = response.responseData;
      		var results = JSON.parse(text)
    	}
    	else {
      		console.log(response.statusCode);
      		console.log(response.error);
    	}
    
    return results
    }
    
    function addDatabasePage(database_id, text) {
    	url = base+"pages"
    	body = {	"parent": { "database_id": database_id },"properties": {"Name": {"title": [{"text": {"content": text}}]}}}
    
    	var http = HTTP.create(); // create HTTP object
    
    	var response = http.request({
    		"url": url,
    		"method": "POST",
      		"data": body,
      		"headers": {
      			"Authorization": "Bearer " + secret,
      			"Notion-Version": version
      			}
    		});
    
    	if (response.success) {
    		var text = response.responseText;
    		var data = response.responseData;
    		var results = JSON.parse(text)
    	}
    	else {
    		console.log(response.statusCode);
    		console.log(response.error);
    	}
    	return results
    }
    
    function addPage(page_id, text) {
    	url = base+"pages"
    	body = {	"parent": { "page_id": page_id },"properties": {"title": [{"text": {"content": text}}]}}
    
    	var http = HTTP.create(); // create HTTP object
    
    	var response = http.request({
    		"url": url,
    		"method": "POST",
      		"data": body,
      		"headers": {
      			"Authorization": "Bearer " + secret,
      			"Notion-Version": version
      			}
    		});
    
    	if (response.success) {
    		var text = response.responseText;
    		var data = response.responseData;
    		var results = JSON.parse(text)
    	}
    	else {
    		console.log(response.statusCode);
    		console.log(response.error);
    	}
    	return results
    }
    
    function addTextBlock(parent_id, text) {
    	url = base+"blocks/" + parent_id + "/children"
    	body = {	"children": [
    					{
    						"object": "block",
    						"type": "paragraph",
    						"paragraph": {
    							"text": [{ "type": "text", "text": { "content": text } }]
    							}
    					}
    				]}
    
    	var http = HTTP.create(); // create HTTP object
    
    	var response = http.request({
    		"url": url,
    		"method": "PATCH",
      		"data": body,
      		"headers": {
      			"Authorization": "Bearer " + secret,
      			"Notion-Version": version
      			}
    		});
    
    	if (response.success) {
    		var text = response.responseText;
    		var data = response.responseData;
    		var results = JSON.parse(text)
    	}
    	else {
    		console.log(response.statusCode);
    		console.log(response.error);
    	}
    	return results
    }
    
  • script

    // Examples of using Notion API using functions from above
    
    // Get Notion secret - needed for all actions
    secret = notionClient()
    
    // Example 1: Add text from a draft to page in Notion
    // If draft is a single line, it just adds a line of text, if multiple lines, it adds a page with the first line as title.
    
    var title = draft.processTemplate("[[line|1]]")
    var note = draft.processTemplate("[[line|2..]]")
    
    if (note) {
    	page = addPage(inbox,title)
    	addTextBlock(page.id,note)
    } else {
    	addTextBlock(inbox,title)
    	}
    
    // Example 2: Add item to database in Notion with the draft title as Action.
    // Need to add the database ID
    
    // addDatabasePage(DATABASE_ID, title)

Options

  • After Success Trash
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.