TwitchClone / src / pages / api / get-asset.ts
get-asset.ts
Raw
import Mux from "@mux/mux-node";
import type { NextApiRequest, NextApiResponse } from "next";
import fetch from "node-fetch";

const MUX_TOKEN_ID = process.env.MUX_TOKEN_ID;
const MUX_TOKEN_SECRET = process.env.MUX_TOKEN_SECRET;
const { Video } = new Mux(
  process.env.MUX_TOKEN_ID || "",
  process.env.MUX_SECRET_KEY || ""
);

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse
) {
  const { uploadId } = req.query;

  try {
    const assets = await Video.Assets.list({
      limit: 1,
      upload_id: uploadId as string,
    });

    if (assets.length === 0) {
      res.status(404).json({ error: "Asset not found" });
    } else {
      res.json(assets[0]);
    }
  } catch (e) {
    console.error("Error getting asset:", e);
    res.status(500).json({ error: "Error getting asset" });
  }
}