Skip to content

Latest commit

 

History

History
 
 

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

Currency Converter

Description

A real-time currency converter that uses the Fixer.io API to fetch current exchange rates and convert between 170+ world currencies. Features an interactive command-line interface with support for all major currencies.

Features

  • Real-time exchange rates from Fixer.io API
  • Support for 170+ currencies
  • Convert between any two supported currencies
  • Display exchange rates
  • List all available currencies
  • Formatted output with thousands separators
  • Error handling and validation
  • Environment variable support for API key
  • Interactive menu system

Stack

  • Language: Python
  • Libraries:
    • requests==2.28.2
  • API: Fixer.io (https://fixer.io/)
  • Complexity: Beginner-Intermediate

Installation

# Install dependencies
pip install -r requirements.txt

# Run the program
python main.py

API Setup

This application requires a free API key from Fixer.io:

  1. Visit https://fixer.io/
  2. Sign up for a free account
  3. Copy your API access key
  4. Use it when prompted, or set as environment variable:

Windows (PowerShell)

$env:FIXER_API_KEY="your_api_key_here"

Linux/macOS

export FIXER_API_KEY="your_api_key_here"

Permanent Setup

Add to your system environment variables or .env file.

Usage

Interactive Mode

python main.py

Example Session

==================================================
           CURRENCY CONVERTER
==================================================

Enter your Fixer.io API key: your_key_here

Fetching exchange rates...
✓ Exchange rates loaded successfully!
  Base currency: EUR
  Last updated: 2024-01-15
  Available currencies: 170

Options:
  1. Convert currency
  2. List available currencies
  3. Exit

Enter your choice (1-3): 1

------------------------------------------------------------
From currency (e.g., USD): USD
To currency (e.g., EUR): INR
Amount: 100

============================================================
           CONVERSION RESULT
============================================================
  100.00 USD = 8,350.50 INR
============================================================
  Exchange Rate: 1 USD = 83.5050 INR
============================================================

How It Works

API Integration

  1. Fetches latest exchange rates from Fixer.io API
  2. Base currency is EUR (Euro)
  3. All rates are relative to EUR
  4. API endpoint: http://data.fixer.io/api/latest?access_key={key}

Conversion Algorithm

# If converting from non-EUR currency
if from_currency != 'EUR':
    amount = amount / rates[from_currency]

# Convert to target currency
if to_currency != 'EUR':
    amount = amount * rates[to_currency]

API Response Format

{
  "success": true,
  "base": "EUR",
  "date": "2024-01-15",
  "rates": {
    "USD": 1.18,
    "GBP": 0.85,
    "INR": 88.5,
    "JPY": 130.2,
    ...
  }
}

Supported Currencies

Major Currencies

  • USD - US Dollar
  • EUR - Euro
  • GBP - British Pound
  • JPY - Japanese Yen
  • CNY - Chinese Yuan
  • INR - Indian Rupee
  • AUD - Australian Dollar
  • CAD - Canadian Dollar
  • CHF - Swiss Franc
  • HKD - Hong Kong Dollar

All Currencies

170+ currencies including:

  • All major world currencies
  • Cryptocurrencies (on some plans)
  • Precious metals (Gold, Silver)
  • Special Drawing Rights (SDR)

Use option 2 in the menu to see the complete list.

Class Structure

CurrencyConverter

Main class for currency conversion operations.

Methods:

  • __init__(url): Initialize and fetch exchange rates
  • convert(from_currency, to_currency, amount): Perform conversion
  • display_conversion(...): Format and display results
  • list_currencies(): Show all available currencies

Key Functions

get_api_key()

  • Checks environment variable FIXER_API_KEY
  • Prompts user if not found
  • Provides instructions for obtaining API key

main()

  • Interactive menu loop
  • Handles user input
  • Manages conversion workflow

Error Handling

  • Network connection errors
  • Invalid API key
  • Unsupported currencies
  • Invalid amount (negative or non-numeric)
  • API rate limiting
  • Timeout errors

Example Conversions

USD to EUR

From: USD
To: EUR
Amount: 1000
Result: 1000.00 USD = 847.46 EUR

GBP to INR

From: GBP
To: INR
Amount: 50
Result: 50.00 GBP = 5,205.88 INR

JPY to USD

From: JPY
To: USD
Amount: 10000
Result: 10,000.00 JPY = 90.70 USD

API Limitations

Free Plan

  • 100 requests per month
  • Base currency: EUR only
  • Monthly data updates
  • No HTTPS support

Paid Plans

  • More requests
  • Multiple base currencies
  • Real-time updates
  • HTTPS support
  • Historical data
  • Time-series data

Customization

Change Base Currency (Paid Plans Only)

url = f"http://data.fixer.io/api/latest?access_key={api_key}&base=USD"

Add Specific Currencies Only

url = f"http://data.fixer.io/api/latest?access_key={api_key}&symbols=USD,GBP,JPY"

Cache Exchange Rates

import json
import time

# Save rates to file
with open('rates_cache.json', 'w') as f:
    json.dump({'rates': self.rates, 'timestamp': time.time()}, f)

# Load from cache if recent
with open('rates_cache.json', 'r') as f:
    cache = json.load(f)
    if time.time() - cache['timestamp'] < 3600:  # 1 hour
        self.rates = cache['rates']

Learning Outcomes

  • REST API integration
  • HTTP requests with requests library
  • JSON data parsing
  • Error handling and validation
  • Class-based design
  • User input handling
  • Environment variables
  • Data formatting and presentation

Future Enhancements

  • GUI version with Tkinter
  • Historical exchange rate charts
  • Currency rate alerts
  • Favorite currency pairs
  • Batch conversions
  • Export results to CSV/Excel
  • Offline mode with cached rates
  • Multi-currency calculator
  • Currency trend analysis
  • Mobile app version

Troubleshooting

"API Error: Invalid authentication credentials"

  • Check your API key is correct
  • Ensure no extra spaces in the key
  • Verify your Fixer.io account is active

"Network error: Connection timeout"

  • Check internet connection
  • Try again after a few moments
  • Check if Fixer.io is accessible

"Currency 'XXX' not supported"

  • Verify currency code is correct (3-letter ISO code)
  • Check if currency is available in your plan
  • Use option 2 to see available currencies

Rate limit exceeded

  • Free plan: 100 requests/month
  • Wait until next month or upgrade plan
  • Implement caching to reduce requests

Alternative APIs

If Fixer.io doesn't meet your needs, consider:

License

This project is open source and available for educational purposes.

Disclaimer

Exchange rates are provided by Fixer.io and may not reflect real-time market rates. This tool is for informational purposes only and should not be used for financial decisions without verification.