openFigi to convert symbols to Tickers

grok3 got me onto openFigi (https://www.openfigi.com/)

Problem. you have a stock SEDOL number or a company name but you dont know the ticker. openFIGI has a really good API for that

its really handy to convert and search for ticker symbols, for example a SEDOL in my case convert a SEDOL like B1GJFH9 to google finance tickers LON:ITP

its a pain to do this manually.

// sedolToTickerWithExchange.mjs
import fetch from 'node-fetch';
import fs from 'fs';

const API_KEY = 'YOUR_API_KEY_HERE'; // Replace with your OpenFIGI API key

// Your SEDOLs from the ii spreadsheet
const sedols = [
  'B1GJFH9', // IDTP (iShares USD TIPS)
  'BR1H1S3', // DFNG (VanEck Defense ETF)
  'BP5GVP9', // EXCS (iShares MSCI EM ex-China)
  'BMZBBT7', // BLK (BlackRock Inc)
  'BRC3N73', // SMCI (Super Micro Computer Inc)
  // Add more as needed
];

// Map OpenFIGI exchCode to Google Finance prefixes
const exchangeMap = {
  'LN': 'LON',    // London Stock Exchange
  'US': 'NYSE',   // Default to NYSE, adjust if NASDAQ
  'UQ': 'NASDAQ', // NASDAQ
  'UN': 'NYSE',
  'UP': 'NASDAQ', // Pink sheets or OTC can vary
  // Add more mappings as you encounter them
};

// Build the request payload
const jobs = sedols.map(sedol => ({
  idType: 'ID_SEDOL',
  idValue: sedol
}));

// Function to call OpenFIGI API
async function mapJobs(jobs) {
  const response = await fetch('https://api.openfigi.com/v3/mapping', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'X-OPENFIGI-APIKEY': API_KEY
    },
    body: JSON.stringify(jobs)
  });

  if (!response.ok) {
    throw new Error(`HTTP error! Status: ${response.status}`);
  }

  return response.json();
}

// Run and format results
async function main() {
  try {
    const results = await mapJobs(jobs);

    // CSV header
    fs.writeFileSync('sedol_tickers.csv', 'SEDOL,TickerWithExchange,Name\n');

    // Process each result
    results.forEach((result, index) => {
      const sedol = sedols[index];
      let output = `${sedol},`;

      if (result.data && result.data.length > 0) {
        const { ticker, exchCode, name } = result.data[0];
        const prefix = exchangeMap[exchCode] || exchCode || 'UNKNOWN';
        const tickerWithExchange = `${prefix}:${ticker}`;
        output += `${tickerWithExchange},${name}`;
        console.log(`${sedol} → ${tickerWithExchange} (${name})`);
      } else {
        output += 'N/A,No mapping found';
        console.log(`${sedol} → No mapping found`);
      }

      fs.appendFileSync('sedol_tickers.csv', `${output}\n`);
    });
  } catch (error) {
    console.error('Error:', error.message);
  }
}

main();