Openpieces logoopenpieces
All contributors
CW

Celine Wang

@cwang

Developer Advocate

Designing developer experiences for AI-native workflows. Building the Notion and Airtable pieces.

New York, USJoined September 10, 2024github/celinewcelinew.dev

1

Piece published

1,678

Total installs

86

Avg AI Score

Pieces by Celine Wang

notion-sync

Communication

Sync pages, databases, and comments between Notion and your workflows. Supports CRUD on pages, query databases with filters, and listen for page updates via webhook polling.

NotionWebhooks
86892cwang15mo ago
1import{serve}from"https://deno.land/std@0.224.0/http/server.ts";
2 
3interfaceNotionPayload{
4action:"query"|"create"|"update";
5databaseId?:string;
6pageId?:string;
7properties?:Record<string,unknown>;
8filter?:Record<string,unknown>;
9}
10 
11constNOTION_TOKEN=Deno.env.get("NOTION_API_TOKEN")!;
12constAPI_BASE="https://api.notion.com/v1";
13 
14asyncfunctionhandleNotion(req:Request):Promise<Response>{
15constpayload:NotionPayload=awaitreq.json();
16 
17switch(payload.action){
18case"query":{
19constres=awaitfetch(
20`${API_BASE}/databases/${payload.databaseId}/query`,
21{
22method:"POST",
23headers:{
24Authorization:`Bearer ${NOTION_TOKEN}`,
25"Notion-Version":"2022-06-28",
26"Content-Type":"application/json",
27},
28body:JSON.stringify({filter:payload.filter}),
29},
30);
31constdata=awaitres.json();
32returnnewResponse(JSON.stringify(data.results??[]),{
33headers:{"content-type":"application/json"},
34});
35}
36case"create":{
37constres=awaitfetch(
38`${API_BASE}/pages`,
39{
40method:"POST",
41headers:{
42Authorization:`Bearer ${NOTION_TOKEN}`,
43"Notion-Version":"2022-06-28",
44"Content-Type":"application/json",
45},
46body:JSON.stringify({
47parent:{database_id:payload.databaseId},
48properties:payload.properties,
49}),
50},
51);
52returnnewResponse(JSON.stringify(awaitres.json()),{
53headers:{"content-type":"application/json"},
54});
55}
56default:
57returnnewResponse("Unknown action",{status:400});
58}
59}
60 
61serve(handleNotion,{port:8080});