Public
Script
Readme

Save To Tana

This val provides a function saveToTana allows the creation of nodes in Tana via their Input API. The parameters are as follows:

  • Token: to access the Tana Input API, you must pass an API token to the function. Obtain an API token from the Tana app and save it as a secret in Val Town.
  • Node: the node that is created within Tana is passed as the second argument and must conform to the shape of an Input API node (see the documentation on github for details.
  • Target node: optionally, you can specify a specific target node by passing a node ID to the function as it's third argument.

Example Usage

One way to use this val is with a web endpoint that you can send data to to have parsed and submitted to Tana as a specific type of node. For example, this val

import { saveToTana } from "https://esm.town/v/nbbaier/saveToTana"; import { APIPlainNode } from "https://esm.town/v/nbbaier/tanaTypes"; import { Hono } from "npm:hono"; const token = Deno.env.get("tanaInputAPI"); export const honoTanaEndpoint = async (req: Request) => { const app = new Hono(); app.get("/", async c => { let { text, url } = c.req.query(); const payload: APIPlainNode = { name: text, children: [ { type: "field", attributeId: "cwi23sOzRSh8", children: [ { dataType: "url", name: url, }, ], }, ], supertags: [], }; const newNode = await saveToTana(token, payload); return c.json({ newNode }); }); return app.fetch(req); };

Combined with a Chrome extension like Rich URL, the above val allows one to send selected text on a page along with that pages URL to Tana via the val's public GET endpoint.

Screenshot 2023-10-27 at 4.13.22 PM.png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import { TanaAPIHelper } from "https://esm.town/v/nbbaier/TanaAPIHelper";
import { tanaConstants } from "https://esm.town/v/nbbaier/tanaConstants";
import { APIPlainNode } from "https://esm.town/v/nbbaier/tanaTypes";
/** saveToTana - creates a node in Tana via the Tana Input API
* @param {string} token - Token for accessing the API.
* @param {APIPlainNode} node - Data node to be created in Tana.
* @param {string} targetNodeId - ID of the target node where the new node will be attached (defaults to the inbox)
* @returns {Promise<APIPlainNode>} - A promise that resolves to the created node.
*/
export const saveToTana = async (
token: string,
node: APIPlainNode,
targetNodeId: string = tanaConstants.inboxNodeId,
) => {
const client = new TanaAPIHelper(token);
const createdNode = await client.createNode(node, targetNodeId);
return createdNode;
};
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
October 27, 2023