Action

Notion functions

Posted by moggy1972, Last update 7 months ago

List of functions and variables to access Notion API

Steps

  • script

    // Credentials and basic variables
    
    // Gets Notion secret from keychain, and sets version and personal environment variables
    
    function notionClient() {
    	var credential = Credential.create("Notion", "Notion API");
    	credential.addPasswordField("secret", "Secret");
    	credential.authorize();
    
    	//Get token
    	var secret = credential.getValue("secret")
    
    	return secret
    }
    
    // Get link for daily note if it exist
    
    
    function getDailyNoteId(dateObject) {
    
    	options = {weekday: 'short', year: 'numeric', month: 'short', day: '2-digit' };
    	dateText = dateObject.toLocaleDateString('en-GB', options).replace(",","")
    	dateText = dateText.substring(0,10)
    	
    	pages = queryPageTitleDb("3564b4cfdbbd47529b8f61ee240427b6", dateText)
    
    	if (pages.results.length == 0) {
    		pageId = inbox
    		} else {
    		pageId = pages.results[0].id
    		}
    
    	return pageId
    }
    
    
    
    // Basic variables
    base = "https://api.notion.com/v1/"
    version ="2021-08-16" //updated
    
    //Personal environment variables
    
    inbox = "14c0f34131574be1b78c9add79264c31" // page that I use as my inbox
    actions = "3564b4cfdbbd47529b8f61ee240427b6" // database for actions - same as notes now - all objects in one place.
    notes = "3564b4cfdbbd47529b8f61ee240427b6" //
    
    // Notes
    // previously used a "require("notion.js")" to include functions from a js file. This seemed to be slower.
    // and also used to get functions from a draft:
    // var s = Draft.find(UUID);
    // this.eval(s.content);
  • script

    // Page functions
    
    // addPage(parentId, title) - adds a subpage or database page, depending on type of page
    // getPage(pageId) - gets details of a page
    // getChildren(pageId)
    // addChildren(pageId,children)
    // addTextBlock(page_id,children)
    
    function addPage(parentId, title) {
    
    	body = {	"parent": { "page_id": parentId },"properties": {"title": [{"text": {"content": title}}]}}
    	url = base+"pages"
    	var http = HTTP.create(); // create HTTP object
    
    	var response = http.request({
    		"url": url,
    		"method": "POST",
      		"data": body,
      		"headers": {
      		"Authorization": "Bearer " + secret
      			}
    		});
    
    	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 getPage(pageId) {
    
    	url = base + "pages/" + pageId
    	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)
    		console.log(text)
    	}
    	else {
    		console.log(response.statusCode);
    		console.log(response.error);
    	}
    	return results
    
    }
    
    function getChildren(pageId) {
    	url = base+"blocks/" + pageId + "/children"
    	console.log(url)
    
    	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)
    		console.log(text)
    	}
    	else {
    		console.log(response.statusCode);
    		console.log(response.error);
    	}
    	return results.results
    }
    
    function addChildren(pageId, children) {
    
    	url = 'https://api.notion.com/v1/blocks/' + pageId + '/children'
    	body = {"children": children}
    	
    	var http = HTTP.create(); // create HTTP object
    	var response = http.request({
    		"url": url,
    		"method": "PATCH",
      		"data": body,
      		"headers": {
      		"Authorization": "Bearer " + secret
      			}
    		});
    
    	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);
    	}
    }
    
    function addTextBlock(parent_id, children) {
    	url = base+"blocks/" + parent_id + "/children"
    	blocks = []
    		for(c = 0; c < children.length; c++){
    		child = {
    						"object": "block",
    						"type": "paragraph",
    						"paragraph": {
    							"text": [{ "type": "text", "text": { "content": children[c]} }]
    							}
    					}
    		blocks.push(child)
    		}
    	var http = HTTP.create(); // create HTTP object
    
    	var response = http.request({
    		"url": url,
    		"method": "PATCH",
      		"data": {"children": blocks},
      		"headers": {
      		"Authorization": "Bearer " + secret
      			}
    		});
    
    	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

    // Database functions
    
    // listDatabases() - returns a list of databases with integrations to API (i.e. not all databases).
    // addDatabasePage(parentId, title) - adds a new database page
    // queryPageTitleDb(databaseId,text) - simple query of a database for an exact match of page title
    // queryDatabase(databaseId,filter,sorts) - database query using submitted filter and sorts
    
    function listDatabases() {
    
    	url = base+"databases"
    	var http = HTTP.create(); // create HTTP object
    
    	var response = http.request({
    		"url": url,
    		"method": "GET",
      		"headers": {
      		"Authorization": "Bearer " + secret
      			}
    		});
    
    	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.results
    }
    
    function addDatabasePageOld(databaseId, title) {
    	url = base+"pages"
    	body = {	"parent": { "database_id": databaseId },"properties": {"Name": {"title": [{"text": {"content": title}}]}}}
    
    	var http = HTTP.create(); // create HTTP object
    
    	var response = http.request({
    		"url": url,
    		"method": "POST",
      		"data": body,
      		"headers": {
      		"Authorization": "Bearer " + secret
      			}
    		});
    
    	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(databaseId, body) {
    	url = base+"pages"
    	var http = HTTP.create(); // create HTTP object
    
    	var response = http.request({
    		"url": url,
    		"method": "POST",
      		"data": body,
      		"headers": {
      		"Authorization": "Bearer " + secret
      			}
    		});
    
    	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 addDatabasePage2(databaseId, title, description, type, icon) {
    	url = base+"pages"
    	body = {	"parent": { "database_id": databaseId },
    			"icon": {"type":"external","external":{"url":icon}},
    			"properties": {
    			"Name": {"title": [{"text": {"content": title}}]},
    			"Type": {"select": {"name":type}},
    			"Description": {"rich_text": [	{"text": {"content": description}}]}	
    				}
    			}
    
    	var http = HTTP.create(); // create HTTP object
    
    	var response = http.request({
    		"url": url,
    		"method": "POST",
      		"data": body,
      		"headers": {
      		"Authorization": "Bearer " + secret
      			}
    		});
    
    	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 editDatabasePage(pageId, body) {
    	url = base+"pages/"+pageId
    	
    	var http = HTTP.create(); // create HTTP object
    
    	var response = http.request({
    		"url": url,
    		"method": "PATCH",
      		"data": body,
      		"headers": {
      		"Authorization": "Bearer " + secret
      			}
    		});
    
    	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 queryPageTitleDb(databaseId,text) {
    
    	filter = {"property": "Name","text": {"equals": text }}
    	body = {"filter":filter}
    
    	url = base + "databases/" + databaseId + "/query"
    	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 queryDatabase(databaseId,filter,sorts) {
    
    	//body = {"filter":filter}
    	body = {"filter":filter, "sorts": sorts, "page_size": 100}
    
    	url = base + "databases/" + databaseId + "/query"
    	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);
    	}
    	//console.log(results.has_more)
    	return results
    }	
    
    function queryDatabasePagination(databaseId,filter,sorts) {
    	// to get result sets of more than 100
    	start_cursor = ""
    	items = []
    
    	do {
    		body = {"filter":filter, "sorts": sorts, "page_size": 100}
    		if (start_cursor != "") {
    			body.start_cursor = start_cursor
    			}
    		url = base + "databases/" + databaseId + "/query"
    		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);
    			}
    		has_more = results.has_more
    		start_cursor = results.next_cursor
    		items = items.concat(results.results)
    	
    	}
    	while (has_more == true)
    
    	return items
    }	
    
    
    
  • script (disabled)

    // Not used
    // addPageOrDatabase(parentId, title) - uses listDatabases function to see whether the parent is a database, and then adds page accordingly. Body is different structure for database pages.
    
    
    function addPageOrDatabase(parentId, title) {
    
    	//check whether parentId is a database
    	databases = listDatabases()
    	databaseList = []
    	for(i = 0; i < databases.length; i++){
    		databaseList.push(databases[i].id)
    		databaseList.push(databases[i].id.replaceAll("-",""))
    	}
    
    	if (databaseList.includes(parentId)) {
    	
    		body = {	"parent": { "database_id": parentId },"properties": {"Name": {"title": [{"text": {"content": title}}]}}}
    		console.log("Is database")
    	} else {
    		body = {	"parent": { "page_id": parentId },"properties": {"title": [{"text": {"content": title}}]}}
    		console.log("Is page")
    	}
    	
    	url = base+"pages"
    	var http = HTTP.create(); // create HTTP object
    
    	var response = http.request({
    		"url": url,
    		"method": "POST",
      		"data": body,
      		"headers": {
      		"Authorization": "Bearer " + secret
      			}
    		});
    
    	if (response.success) {
    		var text = response.responseText;
    		var data = response.responseData;
    		var results = JSON.parse(text)
    		console.log(text)
    	}
    	else {
    		console.log(response.statusCode);
    		console.log(response.error);
    	}
    	return results
    }
    
  • script

    // Icon functions
    
    function getIconUrl(page) {
    	iconUrl = ""
    	if (page.icon != null) {
    		if (page.icon.type == "external") {
    			iconUrl = page.icon.external.url
    			}
    		}
    	return iconUrl
    }
    
    function updateIcon(pageId,iconUrl) {
    	url = base + "pages/" + pageId
    	body = {	"icon": {"type":"external","external":{"url": iconUrl}}}
    		
    	var http = HTTP.create(); // create HTTP object
    	var response = http.request({
    		"url": url,
    		"method": "PATCH",
      		"data": body,
      		"headers": {
      			"Authorization": "Bearer " + secret,
      			"Notion-Version" : version
      		}
    	});
    return response
    }
    

Options

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