gotangible / pages / api / assets.js
assets.js
Raw
import assets from "../../data/assets";

export default async function handler(req, res) {
  const debug = false;

  const {
    query: { owner, cursor },
    method,
  } = req;

  if (debug) {
    res.status(200).json({ status: "success", data: assets });
  } else {
    try {
      let assetsUrl = `https://api.opensea.io/api/v1/assets?owner=${owner}`;
      if (cursor) assetsUrl += `&cursor=${cursor}`;
      const response = await fetch(assetsUrl, {
        method: "GET",
        headers: {
          "X-API-KEY": process.env.OPENSEA_API_KEY
        }
      });
      const data = await response.json();
      res.status(200).json({ status: "success", data: data });
    } catch(err) {
      res.status(200).json({ status: "error", message: "Unable to retrieve assets." });
    }  
  }
}