I have an api router in nextjs. pages/api/checkout
import { coinpayments } from "../../../initCoinpayments";
export default async function handler(req, res) {
const { id } = req.query;
try {
const transactionInfo = await coinpayments.getTx({ txid: id });
res.status(200).json(transactionInfo);
} catch (error) {
console.error("Error fetching transaction info:", error);
res.status(500).json({ error: "An error occurred while fetching transaction info." });
}
}
and then inside pages/checkout
I have:
import Container from "@/components/Container";
import { supabase } from "../../../initSupabase";
import { useUser } from "@supabase/auth-helpers-react";
import { useEffect, useState } from "react";
import PageTitle from "@/components/PageTitle";
import { coinpayments } from "../../../initCoinpayments";
export default function Document() {
const user = useUser(); // Get the authenticated user
const [checkoutData, setCheckoutData] = useState(null);
const [loading, setLoading] = useState(false);
useEffect(() => {
setLoading(true);
async function fetchCheckout() {
if (user) {
let { data: checkoutData, error: checkoutError } = await supabase
.from('checkout')
.select('*')
.eq('user', user.id)
.order('created_at', { ascending: false })
.limit(1);
if (checkoutError) {
console.error("Error fetching checkout data:", checkoutError);
}
if (checkoutData && checkoutData.length > 0) {
const latestCheckout = checkoutData[0];
setCheckoutData(latestCheckout);
if (latestCheckout.txn_id) {
try {
const response = await fetch(`/api/checkout?id=${latestCheckout.txn_id}`);
const transactionInfo = await response.json();
console.log("Transaction Info:", transactionInfo);
// Do something with the transactionInfo, like updating state
} catch (error) {
console.error("Error fetching transaction info:", error);
}
}
}
}
setLoading(false);
}
fetchCheckout();
}, [user, setCheckoutData]);
return (
<>
<Container>
<PageTitle>Checkout</PageTitle>
{checkoutData && <CheckoutInfo data={checkoutData} />}
</Container>
</>
)
}
Which returns a 500 error to the console:
Transaction Info: {error: 'An error occurred while fetching transaction info.'}
What am I doing wrong here? The api key works for creating the transaction but not for this particular function?
Here's the full error from the terminal where I'm running npm run dev
Error fetching transaction info: CoinpaymentsError: This API Key does not have permission to use that command!
at IncomingMessage.<anonymous> (/home/dan/Documents/code/banana/node_modules/coinpayments/dist/internal.js:91:35)
at IncomingMessage.emit (node:events:525:35)
at endReadableNT (node:internal/streams/readable:1358:12)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
extra: {
data: {
error: 'This API Key does not have permission to use that command!',
result: []
}
}
}
I am using the following library (as well as nextjs): https://github.com/OrahKokos/coinpayments#get-transaction-info