Skip to content

Instantly share code, notes, and snippets.

@topherPedersen
Created January 14, 2022 01:48
Show Gist options
  • Save topherPedersen/d912f61088c86c082453a72419eb3ca3 to your computer and use it in GitHub Desktop.
Save topherPedersen/d912f61088c86c082453a72419eb3ca3 to your computer and use it in GitHub Desktop.
Solana 101: Connect to Solana (From Figment.io's Solana 101 Course)
import type {NextApiRequest, NextApiResponse} from 'next';
import {getNodeURL} from '@figment-solana/lib';
import {Connection} from '@solana/web3.js';
export default async function connect(
req: NextApiRequest,
res: NextApiResponse<string>,
) {
try {
const {network} = req.body;
const url = getNodeURL(network);
const connection = new Connection(url, 'confirmed');
const version = await connection.getVersion();
res.status(200).json(version['solana-core']);
} catch (error) {
let errorMessage = error instanceof Error ? error.message : 'Unknown Error';
res.status(500).json(errorMessage);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment