Skip to content

Instantly share code, notes, and snippets.

@danperrout
Last active June 8, 2026 16:50
Show Gist options
  • Select an option

  • Save danperrout/b27197056fa38d0d669332647ab89d7a to your computer and use it in GitHub Desktop.

Select an option

Save danperrout/b27197056fa38d0d669332647ab89d7a to your computer and use it in GitHub Desktop.
API Função TESOURODIRETO Google Sheets
// ============================================================
// COLE SEU TOKEN AQUI (entre as aspas).
// Gere em https://radaropcoes.com → /profile/ → card API.
// ============================================================
const RADAR_TOKEN = "rop_COLE_SEU_TOKEN_AQUI";
// ============================================================
/*
* @return Acessa radaropcoes.com e retorna informações de um título específico do Tesouro Direto
* Endpoint: https://api.radaropcoes.com/bonds/{bondName}
*/
function TESOURODIRETO(bondName, argumento = "r") {
let srcURL = "https://api.radaropcoes.com/bonds/" + encodeURIComponent(bondName);
let response = UrlFetchApp.fetch(srcURL, {
headers: { "Authorization": "Bearer " + RADAR_TOKEN },
muteHttpExceptions: true
});
console.log(srcURL)
if (response.getResponseCode() !== 200) {
throw new Error("Erro ao acessar a API ou título não encontrado.");
}
let bond = JSON.parse(response.getContentText());
// A API responde 200 mesmo com token inválido/ausente/quota: campos numéricos vêm null
// e os textos contêm a mensagem do erro. Surface esse caso pro usuário.
if (bond.treasuryBondCode === null) {
throw new Error(bond.indication);
}
// argumento:
// "r" → valor de resgate
// qualquer outro → valor unitário de investimento
if (argumento === "r") {
return bond.unitaryRedemptionValue;
} else {
return bond.unitaryInvestmentValue;
}
}
@danperrout

danperrout commented Jun 8, 2026 via email

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment