Get Coding Agent to Fetch Live Exchange Rates
This guide shows you how to instruct a coding agent (like GitHub Copilot, Cursor, or an AI pair programmer) to write code that retrieves current exchange rates from a public API. You’ll learn the exact prompts, the key API details to include, and how to verify the result works. This is useful for any project that needs up-to-date currency conversion, from budgeting apps to e-commerce sites.
What You’ll Need
- A coding agent with access to your codebase (or a new project).
- An internet connection (the agent may fetch docs, and the code will call a live API).
- An API key if you choose a provider that requires one (some free tiers don’t).
- Basic familiarity with your project’s language (Python, JavaScript, etc.).
Step-by-Step
- Choose a free exchange-rate API. Common options include exchangerate-api.com, open.er-api.com, or api.exchangerate.host. For this guide, we’ll use the free, no-key endpoint:
https://open.er-api.com/v6/latest/USD. It returns JSON with rates relative to USD. - Open your coding agent in the project folder. If you’re using an IDE extension, make sure it’s active. For a CLI agent, navigate to the project root.
- Write a clear prompt. Specify the language, the endpoint, and the desired output. Example prompt: “In Python, write a function
get_exchange_rate(base_currency, target_currency)that fetches the latest rates fromhttps://open.er-api.com/v6/latest/{base_currency}and returns the rate fortarget_currency. Use therequestslibrary. Handle errors gracefully.” - Include error handling in your prompt. Ask the agent to add try/except blocks, timeouts, and a check for a non-200 HTTP status. This prevents silent failures.
- Ask for a usage example. Prompt: “Also show me an example of calling this function to convert 100 USD to EUR.” This helps you verify the output format.
- Review the generated code. Check that the API URL is correct, the JSON parsing uses the right keys (usually
rates), and the function returns a float. - Run the code. Execute the script in your terminal. If you get a rate (e.g., 0.92 for USD to EUR), it works.
- If you need a specific base currency other than USD, make sure the API supports it (many do). If not, ask the agent to fetch USD rates and convert mathematically (rate = USD_to_target / USD_to_base).
- For production, consider caching. Ask the agent to add a simple cache (e.g., store rates for 10 minutes) to avoid hitting rate limits.
Common Mistakes
- Assuming the API returns rates for all currencies. Some APIs only have a subset. Check the response keys.
- Ignoring the API’s base currency. If you request
/latest/USD, the rates are relative to USD, not to your local currency. Convert accordingly. - Not handling network errors. Without try/except, the script crashes if the API is down.
- Using an API key in the code. If you use a keyed API, ask the agent to read it from an environment variable, not hardcode it.
- Forgetting to install dependencies. If the agent writes
import requests, you needpip install requestsfirst.
Tips
- Be specific in your prompt. Mention the exact function name, input parameters, and return type. The more detail, the better the code.
- Ask for a docstring. It helps future maintainers understand the function.
- Test with a known rate. For example, 1 USD should be about 0.92 EUR (as of 2025). If the result is wildly different, something is off.
- Use a library like
forex-pythonif you want a simpler wrapper, but a direct API call is more transparent. - If the agent suggests a different API, evaluate it for reliability and rate limits before accepting.
Troubleshooting
- Getting a 404 error: Check the URL. The endpoint might require a trailing slash or a different path. Try
https://open.er-api.com/v6/latest/USDin a browser to see the JSON. - Getting a 403 error: The API might require an API key or a User-Agent header. Ask the agent to add a header like
headers = {'User-Agent': 'MyApp/1.0'}. - KeyError: ‘rates’: The response might be nested differently. Print the full JSON to inspect. Some APIs put rates under a
datakey. - Rate is None or NaN: The target currency code might be invalid. Use ISO 4217 codes like ‘EUR’, ‘GBP’, ‘JPY’.
- Code works but is slow: Add a timeout to the request (e.g.,
timeout=10) and consider caching. - The agent generates code that doesn’t run: Check the Python version. If it uses f-strings, you need Python 3.6+. Also, ensure
requestsis installed.
Frequently Asked Questions
Q: Do I need an API key for this?
A: Not for the open.er-api.com endpoint. Some other providers require a free key. If you use one, store it in an environment variable.
Q: Can I get historical exchange rates?
A: Yes, some APIs offer historical endpoints (e.g., /v6/history/USD/2024-01-01). Ask your agent to modify the URL accordingly.
Q: How often are the rates updated?
A: Most free APIs update once a day. For real-time rates, you’d need a paid plan.
Q: What if I need a currency not in the API?
A: Some APIs support a limited set. If a currency is missing, you might need a different provider or to use a conversion via a common base.
Q: Can I use this in a web app?
A: Yes, but be careful about exposing your API key. Make the request server-side or use a proxy.
Q: Why does the rate differ from Google’s?
A: Different sources use different rates (mid-market vs. buy/sell). The API provides a mid-market rate, which is fine for most applications.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.