Skip to content

Instantly share code, notes, and snippets.

@tiero
Forked from bumi/nwc-openai.mjs
Created July 16, 2023 22:53
Show Gist options
  • Save tiero/f3e7b511a8bb99f2b5f603418b808b5e to your computer and use it in GitHub Desktop.
Save tiero/f3e7b511a8bb99f2b5f603418b808b5e to your computer and use it in GitHub Desktop.
NWC, L402, OpenAI example
import { fetchWithL402 } from "alby-tools";
import { webln } from "alby-js-sdk";
import 'websocket-polyfill';
import * as crypto from 'crypto'; // or 'node:crypto'
globalThis.crypto = crypto;
const nwcURL = process.env.NWC_URL;
async function main() {
const nwc = new webln.NWC({ nostrWalletConnectUrl: nwcURL });
nwc.on("sendPayment", console.log);
const payload = {
"prompt": "make me a sandwitch",
"n": 1,
"size": "1024x1024"
};
const res = await fetchWithL402(
"https://matador-ai.replit.app/v1/images/generations",
{
method: "POST",
body: JSON.stringify(payload),
mode: "cors",
headers: { "Content-Type": "application/json", "Accept": "application/json" }
}, {
header_key: "L402",
webln: nwc
}
);
console.log(res);
const data = await res.json();
console.log(data);
nwc.close();
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment