Skip to content

Commit

Permalink
Nova rota api crypto (Beta)
Browse files Browse the repository at this point in the history
Uma nova rota para criptomoedas está sendo testada e implementada, pode ter bugs e problemas de formatação.
  • Loading branch information
GeovaneDev committed Feb 11, 2024
1 parent 6a4cef3 commit cd9ee92
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
27 changes: 27 additions & 0 deletions pages/api/crypto/coins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import axios from 'axios';

export default async function handler(request, response) {
try {
const apiKey = process.env.APIKEYCRYPTO;

const response = await axios.get('https://api.coingecko.com/api/v3/coins/markets', {
params: {
vs_currency: 'brl',
order: 'market_cap_desc',
per_page: 15000,
page: 1,
sparkline: false,
x_cg_demo_api_key: apiKey
},
});

response.setHeader('Vercel-CDN-Cache-Control', 'max-age=86400');
response.setHeader('CDN-Cache-Control', 'max-age=86400');
response.setHeader('Cache-Control', 'max-age=86400');

const data = response.data;
response.status(200).json(data);
} catch (error) {
response.status(500).json({ error: error.message });
}
}
31 changes: 31 additions & 0 deletions pages/api/crypto/price/[coin].js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import axios from 'axios';

export default async function handler(request, response) {
try {
let { coin } = request.query;
const apiKey = process.env.APIKEYCRYPTO;

if (typeof coin !== 'string') {
throw new Error('Coin parameter must be a string');
}

coin = coin.toLowerCase();

const response = await axios.get(`https://api.coingecko.com/api/v3/simple/price`, {
params: {
ids: `${coin}`,
vs_currencies: "brl",
x_cg_demo_api_key: apiKey
},
});

response.setHeader('Vercel-CDN-Cache-Control', 'max-age=86400');
response.setHeader('CDN-Cache-Control', 'max-age=86400');
response.setHeader('Cache-Control', 'max-age=86400');

const data = response.data;
response.status(200).json(data);
} catch (error) {
response.status(500).json({ error: error.message });
}
}

0 comments on commit cd9ee92

Please sign in to comment.