I have trouble with API when Build A Web3 DEX

im fully clone code from https://github.com/IAmJaysWay/dexFinal
but after replace MORALIS_KEY then run npm install, npm i express at server and client after than
i cant use swap feature although web is running good! image|690x156

1 more, in video i watched on youtube, i didnt see the author use his 1inch API_key can someone explain why?

im newbie with this can someone help me with this errors?

thank you guys! love you all.

Hi @nkhoa0404

As per the error in image, it seems like the server is not running. Please check if the server is active or if there are any errors in the server console.

It could be a new addition from 1inch API. In the past we do not need an API key to use it.

1 Like

NEW UPDATE:

thank you for your help and noticing my post! But im already fixed the CORS error by set up a proxy server like this

Another error
i have just researched about it on stackoverflow, they said that, because of security and firewall of my browser so i have to create a proxy server to bridge with API, but now i dont know how to bridge server with API

They gave me a simple code to replace like this, but it still got “404 NOT FOUND” instead.

my code:


Are you sure that your server is running on port 3002?

1 Like

yeah, im sure with that
turn of server 3002:

turn on 3002:
image

As per this error message “receiving end does not existing”, it also mean then the API endpoint path is incorrect.

Can you please verify if this path exist in your server code?
image

i edited nothing at server, just add proxyServer to fix cors
About that path, its author’s code
 i have just update new link url of 1inch’s api /swap/v6.0/
This is my code at server:

const express = require(“express”);
const Moralis = require(“moralis”).default;
const { createProxyMiddleware } = require(‘http-proxy-middleware’);
const cors = require(“cors”);
require(“dotenv”).config();
const axios = require(‘axios’);
const app = express();
const port = 3001;

// Middleware để cho phĂ©p táș„t cáșŁ cĂĄc nguồn truy cáș­p (CORS)
app.use(cors());
app.use(express.json());

// Proxy middleware để chuyển tiáșżp yĂȘu cáș§u đáșżn 1inch API
app.use(
“/swap”,
createProxyMiddleware({
target: “https://api.1inch.dev”,
changeOrigin: true,
pathRewrite: {
‘^/swap’: ‘’, // Rewrites the URL for the target
},
onProxyReq: (proxyReq) => {
proxyReq.setHeader(
“Authorization”,
Bearer ${process.env.REACT_APP_1INCH_KEY}
);
},
})
);

app.get("/tokenPrice", async (req, res) => {
const { query } = req;
try {
const responseOne = await Moralis.EvmApi.token.getTokenPrice({
address: query.addressOne,
});
const responseTwo = await Moralis.EvmApi.token.getTokenPrice({
address: query.addressTwo,
});

const usdPrices = {
  tokenOne: responseOne.raw.usdPrice,
  tokenTwo: responseTwo.raw.usdPrice,
  ratio: responseOne.raw.usdPrice / responseTwo.raw.usdPrice,
};
return res.status(200).json(usdPrices);

} catch (error) {
console.error(error);
return res.status(500).json({ message: “Error fetching token prices” });
}
});

// Khởi động Moralis vĂ  backend server
Moralis.start({
apiKey: process.env.MORALIS_KEY,
}).then(() => {
app.listen(port, () => {
console.log(Listening for API Calls on port ${port});
});
});