Error: Forbidden, got this in discord when putting in my own API url

I just made my first API in express and when I’m trying to get the image on a discord bot, my console returns:

    at Request.callback (F:\Discord Bots\Command Testing Bot\node_modules\superagent\lib\node\index.js:883:15)
    at IncomingMessage.<anonymous> (F:\Discord Bots\Command Testing Bot\node_modules\superagent\lib\node\index.js:1127:20)
    at IncomingMessage.emit (events.js:327:22)
    at endReadableNT (_stream_readable.js:1221:12)
    at processTicksAndRejections (internal/process/task_queues.js:84:21)
(node:8944) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:8944) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

The code for the api is:

const app = express();
const multer = require("multer");
const path = require("path");
const axios = require("axios");
app.use(require('express-title')());


app.use(express.static("public"));
app.get("/", function(req, res) {
  //res.status(200).sendFile(__dirname + "/views/index.html"); //make index.html a main dashboard
  res.redirect('/api/v1/endpoints') //Keep this until main dashboard is completed
});

app.use(express.static("public"));
app.get("/api/v1/endpoints", function(req, res) {
  res.status(200)
  .sendFile(__dirname + "/views/endpoints.json")
});

app.use(express.static("public"));
app.get("/api/v1/twintails", function(req, res) {

    const twinimgLimit = 145; 
    const twinimgNumber = Math.floor(Math.random()*twinimgLimit);
    var twinurl = "https://media.publit.io/file/Twintails/" + twinimgNumber + ".jpg"; //only for jpg images
  
  /*
const iimgLimit = 7; 
    const iimgNumber = Math.floor(Math.random()*iimgLimit);
    var twinurl2 = "https://media.publit.io/file/Twintails/" + iimgNumber + ".png";
  */ //For png images
  
  res.status(200)
  res.json({ 
    
    url: twinurl
  
  });
});

app.use(express.static("public"));
app.get("/api/v1/loli", function(req, res) {

  const loliimgLimit = 109; 
    const loliimgNumber = Math.floor(Math.random()*loliimgLimit);
    var loliurl = "https://media.publit.io/file/Loli/" + loliimgNumber + ".jpg";
  
  res.status(200)
  res.json({ 
    
    url: loliurl
  
  });
});

 const listener = app.listen(4000, function() 
{ console.log('Your app is listening on port ' + listener.address().port); });

And finally, the code for my discord bot command is:

const superagent = require('superagent');
const config = require('../../utils/config.json')

module.exports.run = async(client, message, args, utils) => {

    const { body } = await superagent
    .get('https://loli-api.glitch.me/api/v1/twintails');

    const embed = new MessageEmbed()
    .setColor('#aaaaaa')
    .setImage(body.url);

    message.channel.send({ embed });

}

module.exports.help = {
    aliases: [],
    name: 'loli',
    description: 'bruh',
    usage: `${config.prefix}loli`
}

module.exports.config = {
    args: false,
    restricted: false, /* Can only owner use the command? Could be false or true. */
    category: 'Utils'
}

I would like to know what’s wrong in my API or what’s wrong in my discord bot. Just any way to display images from the API to the discord chat.

API: https://loli-api.glitch.me/api/v1/endpoints

In your api file, you need to put

const express = require("express");

before you define app. That might not be the problem, but it could be a problem. Also, you only need to put

 app.use(express.static("public"))

once :slightly_smiling_face: smile:
I don’t know whether this will help anything, but it might do.

1 Like

Thanks! I’ll see.

It looks like you just didn’t copy in the first line :smile:
Can you try listening on port 3000?

2 Likes