Back to APIs list

NASA API examples & templates

Use these vals as a playground to view and fork NASA API examples and templates on Val Town. Run any example below or find templates that can be used as a pre-built solution.
1
2
3
4
5
6
7
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
// Returns NASA's Astronomy Picture of the Day (APOD)
export const nasaAPOD = await fetchJSON("https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY");
console.log(nasaAPOD.url);
// Forked from @iBrokeit.nasaAPOD
1
2
3
4
5
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
// Returns NASA's Astronomy Picture of the Day (APOD)
export const nasaAPOD = fetchJSON("cutt.ly/T7ksirK");
// Forked from @iBrokeit.nasaAPOD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
export let date_me_docs_cache = [{
"Name": "Kamil",
"Profile": "https://docs.google.com/document/d/1kQgNZ7ikgKAxyxbsLSC2bBhXQgxv6Em340-ht5R5sjo/edit?usp=sharing",
"Gender": ["M"],
"Age": "24",
"Contact": "postreduxx gmail",
"LastUpdated": "2023-10-19T12:50:00.000Z",
"InterestedIn": ["M"],
"Location": ["Central Europe"],
"Style": ["mono"],
"LocationFlexibility": "Some",
"Community": ["Rationalism"],
}, {
"Name": "Zak Kallenborn",
"Profile": "zkallenborn.com/dateme",
"Gender": ["M"],
"Age": "33",
"Contact": "zkallenborn@gmail.com",
"LastUpdated": "2023-10-18T04:22:00.000Z",
"InterestedIn": ["F"],
"Location": ["DC"],
"Style": ["mono"],
"LocationFlexibility": "Some",
"Community": ["Tech", "EA"],
}, {
"Name": "Frazer Kirkman",
"Profile": "https://www.okcupid.com/profile/frazerkirkman",
"Gender": ["M"],
"Age": "44",
"Contact": "Frazerkirkman@gmail.com",
"LastUpdated": "2023-10-16T20:02:00.000Z",
"InterestedIn": ["F"],
"Location": ["San Francisco Bay Area"],
"Style": ["poly"],
"LocationFlexibility": "Some",
"Community": ["Tech", "Rationalism", "EA"],
}, {
"Name": "Mitchell Reynolds",
"Profile": "https://tundra-shell-d74.notion.site/Mitchell-s-Date-Me-Doc-9325999704f347a4b2515df774b3c7ff",
"Gender": ["M"],
"Age": "32",
"Contact": "https://www.facebook.com/mitchellsreynolds/",
"LastUpdated": "2023-10-14T06:43:00.000Z",
"InterestedIn": ["F"],
"Location": ["San Francisco Bay Area", "Berkeley"],
"Style": ["mono"],
"LocationFlexibility": "Flexible",
"Community": ["EA"],
}, {
"Name": "Sarah Hirner",
"Profile": "https://docs.google.com/document/d/1b3ZmRGXVfqiI9qcU55opVSxzDOFW5PYUNB_GUncbuZ0/edit?usp=sharing",
"Gender": ["F"],
"Age": "33",
"Contact": "shirner244@gmail.com",
"LastUpdated": "2023-10-13T18:05:00.000Z",
"InterestedIn": ["M"],
"Location": ["MA"],
"Style": ["mono"],
"LocationFlexibility": "None",
"Community": ["EA"],
}, {
"Name": "Marta",
"Profile": "https://martakrzeminska.notion.site/Date-Me-17435416720c439b8d6725d690808f55?pvs=4",
"Gender": ["F"],
"Age": "35",
"Contact": "SoYouWantToDateMarta[at]gmail[dot]com ",
"LastUpdated": "2023-10-11T10:18:00.000Z",
"InterestedIn": ["M"],
"Location": ["Central Europe"],
"Style": ["poly"],
"LocationFlexibility": "Flexible",
"Community": ["EA", "Rationalism", "Tech"],
}, {
"Name": "Vasco Grilo",
"Profile": "https://www.okcupid.com/profile/12671992320363212181/photos",
"Gender": ["M"],
"Age": "25",
"Contact": "+351918368684",
"LastUpdated": "2023-10-10T22:02:00.000Z",
"InterestedIn": ["F"],
"Location": ["Central Europe"],
"Style": ["mono"],
"LocationFlexibility": "Some",
"Community": ["EA"],
}, {
"Name": "David",
"Profile": "https://docs.google.com/document/d/1vhHtWBS-tS0bYamYxqr8KJNo6gxjE_0Kbzd6SsVt9ws/edit?usp=sharing",
"Gender": ["M"],
"Age": "34",
"Contact": "see doc",
"LastUpdated": "2023-10-10T18:27:00.000Z",
"InterestedIn": ["F", "M", "NB"],
"Location": ["London"],
"Style": ["mono", "poly"],
"LocationFlexibility": "Some",
"Community": ["Rationalism", "EA"],
}, {
"Name": "Jacques",
"Profile": "https://jacquesthibodeau.com/lets-go-on-a-date/",
"Gender": ["M"],
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import * as base64 from "https://deno.land/std/encoding/base64.ts";
const bytes = base64.decodeBase64(
// base64 encoded wasm module
"AGFzbQEAAAABiwEUYAJ/fwF/YAJ/fwBgA39/fwF/YAF/AX9gA39/fwBgAX8AYAR/f39/AGAAAX9gBX9/f39/AGAEf39/fwF/YAZ/f39/f38Bf2AHf39/f39/fwF/YAZ+f39/f38BfmADfn9/AX9gB39/f39/f38AYAJ+fwBgA39+fgBgBH9+fn8AYAV/f39/fwF/YAAAAjcBGF9fd2JpbmRnZW5fcGxhY2Vob2xkZXJfXxpfX3diZ19sb2dfM
);
let imports: any = {};
imports["__wbindgen_placeholder__"] = imports;
let wasm;
const { TextDecoder, TextEncoder } = globalThis;
let cachedTextDecoder = new TextDecoder("utf-8", { ignoreBOM: true, fatal: true });
cachedTextDecoder.decode();
let cachedUint8Memory0 = null;
function getUint8Memory0() {
if (cachedUint8Memory0 === null || cachedUint8Memory0.byteLength === 0) {
cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer);
}
return cachedUint8Memory0;
}
function getStringFromWasm0(ptr, len) {
ptr = ptr >>> 0;
return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
}
let WASM_VECTOR_LEN = 0;
let cachedTextEncoder = new TextEncoder("utf-8");
const encodeString = typeof cachedTextEncoder.encodeInto === "function"
? function(arg, view) {
return cachedTextEncoder.encodeInto(arg, view);
}
: function(arg, view) {
const buf = cachedTextEncoder.encode(arg);
view.set(buf);
return {
read: arg.length,
written: buf.length,
};
};
function passStringToWasm0(arg, malloc, realloc) {
if (realloc === undefined) {
const buf = cachedTextEncoder.encode(arg);
const ptr = malloc(buf.length, 1) >>> 0;
getUint8Memory0().subarray(ptr, ptr + buf.length).set(buf);
WASM_VECTOR_LEN = buf.length;
return ptr;
}
let len = arg.length;
let ptr = malloc(len, 1) >>> 0;
const mem = getUint8Memory0();
let offset = 0;
for (; offset < len; offset++) {
const code = arg.charCodeAt(offset);
if (code > 0x7F) break;
mem[ptr + offset] = code;
}
if (offset !== len) {
if (offset !== 0) {
arg = arg.slice(offset);
}
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
const view = getUint8Memory0().subarray(ptr + offset, ptr + len);
const ret = encodeString(arg, view);
offset += ret.written;
}
WASM_VECTOR_LEN = offset;
return ptr;
}
let cachedInt32Memory0 = null;
function getInt32Memory0() {
if (cachedInt32Memory0 === null || cachedInt32Memory0.byteLength === 0) {
cachedInt32Memory0 = new Int32Array(wasm.memory.buffer);
}
return cachedInt32Memory0;
}
const heap = new Array(128).fill(undefined);
heap.push(undefined, null, true, false);
function getObject(idx) {
return heap[idx];
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import { email } from "https://esm.town/v/std/email?v=9";
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
export const email_apod = (async () => {
const marked = await import("npm:marked");
const apod = await fetchJSON("https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY");
const output = `
<h1> ${apod.title}</h1>
<p>${apod.explanation}</p>
<img src="${apod.url}" />
`;
const res = await email({
subject: `APOD: ${apod.title}`,
html: output,
});
return res;
})();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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 tanaSave = async (req: Request) => {
const app = new Hono();
app.get("/save", async c => {
let { text, url } = c.req.query();
const payload: APIPlainNode = {
name: text,
children: [
{ name: "child 1" },
{ type: "field", attributeId: "cwi23sOzRSh8" },
],
};
const newNode = await saveToTana(token, payload);
return c.json({ newNode });
});
return app.fetch(req);
};
1
2
3
4
5
6
7
8
9
10
11
12
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON";
export const nasaImageDetails = async () => {
const nasaAPOD = await fetchJSON("https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY");
let nasaImageHtml = nasaAPOD.hdurl
? `<img width="100%" src="${nasaAPOD.hdurl}"/>`
: nasaAPOD.media_type === "video"
? `<iframe width="100%" height="100%" src="${nasaAPOD.url}" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>`
: `<p>Unknown Type</p><br/><code><pre>${JSON.stringify(nasaAPOD, null, 2)}</pre></code>`;
nasaImageHtml += `<p>${nasaAPOD.explanation}</p>`;
return { html: nasaImageHtml, nasaAPOD };
};
Runs every 1 days
Fork
1
2
3
4
5
6
7
8
9
10
11
import { email } from "https://esm.town/v/std/email";
import { nasaImageDetails } from "https://esm.town/v/wallek/nasaImageDetails";
export const todaysNasaMail = async () => {
const { html, nasaAPOD } = await nasaImageDetails();
console.log({ html, nasaAPOD });
const mailMeTodaysNasaImage = await email({
html: "<a href=\"https://wallek-nasaImage.web.val.run\">Weblink</a><br/><br/><br/>" + html,
subject: `Daily NASA – ${nasaAPOD.title}`,
});
};
1
2
3
4
5
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
// Returns NASA's Astronomy Picture of the Day (APOD)
export const nasaAPOD = fetchJSON("cutt.ly/T7ksirK");
// Forked from @iBrokeit.nasaAPOD
1
2
3
4
5
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
// Returns NASA's Astronomy Picture of the Day (APOD)
export const nasaAPOD = fetchJSON("cutt.ly/T7ksirK");
// Forked from @iBrokeit.nasaAPOD
1
2
3
4
5
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
// Returns NASA's Astronomy Picture of the Day (APOD)
export const nasaAPOD = fetchJSON("cutt.ly/T7ksirK");
// Forked from @iBrokeit.nasaAPOD
1
2
3
4
5
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
// Returns NASA's Astronomy Picture of the Day (APOD)
export const nasaAPOD = fetchJSON("cutt.ly/T7ksirK");
// Forked from @iBrokeit.nasaAPOD

This uses the now-deprecated express handler. Comment on this val if you'd like me to update it. Or send me a PR :)

Readme
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON";
export async function hootsuiteNasa(req: express.Request, res: express.Response) {
const NASA_ROOT = "https://images-api.nasa.gov";
const alphaNumericSpaces = new RegExp("^[a-zA-Z0-9\\s-_]+$");
const acceptableRedirectUri = new RegExp(
"^https:\/\/api-auth.hootsuite.com\/app-directory\/apps\/[0-9]+\/oauth2\/callback$",
);
if (req.path === "/v1/media") {
var data = [];
var query = req.query.query || "africa";
if (!alphaNumericSpaces.test(query)) { // special chars in search
res.send('{"data":[],"metadata":{"cursor":{}}}');
}
var uri = NASA_ROOT + "/search?q=" + query + "&media_type=image";
if (req.query.cursor) {
uri = decodeURIComponent(req.query.cursor);
}
var results = await fetchJSON(uri);
var i;
for (i = 0; i < results.collection.items.length; i++) {
if (results.collection.items[i].links) {
var result = {
"id": results.collection.items[i].data[0].nasa_id,
"name": results.collection.items[i].data[0].title,
"mediaType": "Image",
"mimeType": "image/jpeg",
"original": {
"url": results.collection.items[i].links[0].href,
},
"thumbnail": {
"url": results.collection.items[i].links[0].href,
},
};
data.push(result);
}
}
var out = {
"data": data,
"metadata": {
"cursor": {},
},
};
if (results.collection.links) {
for (i = 0; i < results.collection.links.length; i++) {
if (results.collection.links[i].rel === "next") {
out.metadata.cursor.next = encodeURIComponent(
results.collection.links[i].href,
);
}
}
}
res.send(JSON.stringify(out));
}
else if (req.path === "/v1/status") {
res.status(200);
}
else if (req.path === "/authorize") {
var state = req.query.state;
var redirect_uri = decodeURIComponent(req.query.redirect_uri);
if (
alphaNumericSpaces.test(state) && acceptableRedirectUri.test(redirect_uri)
) {
res.redirect(redirect_uri + "?code=example" + "&state=" + state);
}
else {
res.status(403);
}
}
else if (req.path === "/token") {
var exampleToken = {
"access_token": "access_token",
"token_type": "bearer",
"expires_in": 7200,
"refresh_token": "refresh_token",
};
res.send(JSON.stringify(exampleToken));
}
else {
res.send("unknown path");
}
}
1
2
3
4
5
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
// Returns NASA's Astronomy Picture of the Day (APOD)
export const nasaAPOD = fetchJSON("cutt.ly/T7ksirK");
// Forked from @iBrokeit.nasaAPOD
1
2
3
4
5
6
7
8
9
import { nasaImageDetails } from "https://esm.town/v/wallek/nasaImageDetails";
// View at https://wallek-nasaImage.web.val.run
export async function nasaImage() {
const { html } = await nasaImageDetails();
return new Response(html, {
headers: { "Content-Type": "text/html" },
});
}