Skip to content

Build a currency converter with Python

Build a Flask form that converts an amount with a dated reference rate and displays the published date beside the result.

Working example

ParseAPI
Python

Set up ParseAPI

Set PARSEAPI_KEY in your server environment.

Install the Python web app dependencies in a virtual environment.

Terminal
python3 -m venv .venv
source .venv/bin/activate
python3 -m pip install Flask==3.1.3 httpx==0.28.1
export PARSEAPI_KEY='YOUR_SECRET_API_KEY'
Get an API key ↗

Put currency conversion in a web form

Build a Python page where someone enters an amount, selects two currencies and chooses a date. The server fetches the reference rate, calculates with Decimal, and shows both the converted amount and the rate's actual publication date.

The complete app uses Flask for the form and template. The secret ParseAPI key stays on the server.

1. Read the amount, pair and date

Keep the amount as decimal text until it becomes a Decimal. The example accepts up to nine whole digits and four decimal places, including zero and negative amounts. It offers a short list of supported currencies and rejects malformed or future dates before making a request.

Flask renders the current value into the amount field:

HTML
<label for="amount">Amount</label>
<input id="amount" name="amount" inputmode="decimal" value="{{ values.amount }}"
       maxlength="15" aria-describedby="amount-error"
       aria-invalid="{{ 'true' if errors.amount else 'false' }}" required>

When the form posts to /, the handler reads these named inputs:

Python
values = {
    "amount": request.form.get("amount", ""),
    "base": request.form.get("base", ""),
    "quote": request.form.get("quote", ""),
    "day": request.form.get("day", ""),
}

Re-render the submitted values when a field needs correction. The app also checks a session-bound CSRF token before requesting a rate.

2. Fetch the dated reference rate

Request the selected pair with httpx. The HTTP client sends X-API-Version: 2.3.0 and your secret key on both requests:

Python
response = client.get(f"/currency/{base}/{quote}", params={"date": day.isoformat()})
response.raise_for_status()
exchange = response.json()

The rate can be dated earlier than the requested day. Weekends and holidays use the most recent published rate on or before it; missing history remains an error rather than falling back to today's rate.

Ask Currency for the target's usual decimal precision:

Python
response = client.get(f"/currency/{quote}")
response.raise_for_status()
currency = response.json()

This second lookup supplies a fact needed to round the displayed amount. Zero digits is valid, as with JPY. Missing precision leaves conversion unavailable instead of guessing two places.

3. Calculate with the returned facts

Read the multiplier, applied date and precision:

Python
rate = Decimal(str(exchange["rate"]))
rate_date = exchange["date"]
digits = currency["digits"]

Decimal(str(...)) takes the returned rate into decimal arithmetic. The original form amount also becomes a Decimal, so multiplication does not use binary floating-point amounts.

Round once, at the target currency's precision:

Python
converted = (amount * rate).quantize(
    Decimal(1).scaleb(-digits), rounding=ROUND_HALF_EVEN
)

ROUND_HALF_EVEN resolves an exact tie to the nearest even final digit. Choose the rounding policy your application needs. The complete page formats that value and displays its currency and rate_date beside it.

4. Keep the result attached to its inputs

The result names the original amount and currency, the target currency, the multiplier and the published rate date. A zero result remains visible. Invalid input, unavailable history, request errors and malformed responses produce an error state with the person's entries still in the form.

The handler passes result into the page template, where the converted value and applied rate date stay together:

HTML
<p class="amount">{{ result.converted }} {{ result.quote }}</p>
<p>Rate: {{ result.rate }} · Published {{ result.rate_date }}</p>

These are reference estimates. Card charges, fees and settlement rates can differ. See the Currency reference for rate coverage.

The complete app reuses one HTTP client per submitted conversion, with a ten-second timeout and no automatic retries. A failed first request prevents the second lookup.

The example generates a session secret when it starts. Use a stable APP_SECRET when adapting it to your own deployment. Currency selection here belongs to this conversion; the currency picker teaches an app-wide preference.

Complete example

Use the complete implementation as a reference when adding this feature to your app. It includes the validation and failure handling around the calls above.

View and copy the complete source
Try the complete example on your computer

After completing the setup above, save the source in your application folder and start the app:

Terminal
python3 python-currency-converter.python.py

Open 127.0.0.1:8000 to use the form. The server keeps your secret key out of the page.

For every field and parameter, see Currency API reference.

Build something else

All tutorials →