Convert USD to EUR in Swift

1 min readMay 21, 2021


If your websites or programs provide service to a wide variety of countries, integrating an instant price conversion tool is an easy way to improve the customer experience. In this brief tutorial, we will discuss how you can use an API in Swift to convert prices between different currencies like USD and EUR by leveraging constantly updated exchange rate data.

To use this function, you will need to input the source currency code (i.e. USD), the destination currency code (i.e. EUR), the source price, and your API key into the following code:

import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
var semaphore = DispatchSemaphore (value: 0)let parameters = "\"<double>\""
let postData = .utf8)
var request = URLRequest(url: URL(string: "<string>/to/<string>")!,timeoutInterval: Double.infinity)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("YOUR-API-KEY-HERE", forHTTPHeaderField: "Apikey")
request.httpMethod = "POST"
request.httpBody = postData
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data else {
print(String(describing: error))
print(String(data: data, encoding: .utf8)!)

With this simple process, your system can automatically perform price conversions, enabling a better flow of business.




There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.