Skip to content

Instantly share code, notes, and snippets.

@8bitgentleman
Last active September 27, 2023 17:28
Show Gist options
  • Save 8bitgentleman/75561ac116b5b925fd58ff595389d591 to your computer and use it in GitHub Desktop.
Save 8bitgentleman/75561ac116b5b925fd58ff595389d591 to your computer and use it in GitHub Desktop.
Example python functions for accessing the Roam Research Alpha Backend API
import requests
import json
import pprint
GRAPH_NAME = "GRAPH"
API_TOKEN_READ = "TOKEN"
API_TOKEN_WRITE = 'TOKEN'
BASE_URL = "https://api.roamresearch.com"
ENDPOINT_q = f"/api/graph/{GRAPH_NAME}/q"
ENDPOINT_pull = f"/api/graph/{GRAPH_NAME}/pull"
ENDPOINT_write = f"/api/graph/{GRAPH_NAME}/write"
def createBlock(parent, block):
headers = {
"Accept": "application/json",
"Authorization": f"Bearer {API_TOKEN_WRITE}",
"Content-Type": "application/json",
}
endPoint = f'https://api.roamresearch.com/api/graph/{GRAPH_NAME}/write'
data = {
"action": "create-block",
"location": {
"parent-uid": parent,
"order": block.order},
"block": {
"string": block.string,
"uid": block.uid,
"open": block.blockOpen,
"heading": block.heading,
"text-align": block.textAlign,
"children-view-type": block.childView}
}
pp.pprint(data)
urlBase = endPoint.format(graph= GRAPH_NAME)
redirect = requests.post(url=urlBase)
url = redirect.url
data = json.dumps(data)
r = requests.post(url=url, headers=headers, data=data)
return block.uid
def createPage(title, uid):
headers = {
"Accept": "application/json",
"Authorization": f"Bearer {API_TOKEN_WRITE}",
"Content-Type": "application/json",
}
endPoint = f'https://api.roamresearch.com/api/graph/{GRAPH_NAME}/write'
data = {
"action": "create-page",
"page": {
"title": title,
"uid": uid}
}
urlBase = endPoint.format(graph= GRAPH_NAME)
redirect = requests.post(url=urlBase)
url = redirect.url
data = json.dumps(data)
r = requests.post(url=url, headers=headers, data=data)
return uid
def queryGraph():
headers = {
"Accept": "application/json",
"Authorization": f"Bearer {API_TOKEN_READ}",
"Content-Type": "application/json",
}
endPoint = f'https://api.roamresearch.com/api/graph/{GRAPH_NAME}/q'
query = '''[:find (pull ?e [*])
:in $ ?PAGE
:where
[?e :node/title ?PAGE]
]'''
query = query.replace("\n", " ")
data = {
"query" : query,
"args": ["April 26th, 2023"]
}
urlBase = endPoint.format(graph= GRAPH_NAME)
print(urlBase)
redirect = requests.post(url=urlBase)
pp.pprint(redirect)
url = redirect.url
data = json.dumps(data)
r = requests.post(url=url, headers=headers, data=data)
pp.pprint(r.status_code)
return r.json()
@8bitgentleman
Copy link
Author

@jagtesh this is an older snippet, the best way now is to use the official SDK. It's pretty straightforward even though it doesn't have any docs yet

https://github.com/Roam-Research/backend-sdks/tree/master/python

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment