Home

3 min read

Currency Conversion API

In the world of eCommerce, providing a seamless payment experience is vital for conversion rates. One often overlooked aspect is currency conversion. Customers from various regions prefer to see prices in their local currency, removing any hesitation linked to currency exchange. This guide explains how the currency API endpoint can be integrated into Stripe Checkout to achieve high conversion rates effortlessly.

Why Implement Currency Conversion?

Research shows that displaying prices in local currencies can significantly increase checkout conversions. It helps:

Setting Up the Currency API

Step 1: Get API Access

First, sign up for an account and add credits to use our currency conversion API key Spider Credits. Obtain your API key upon registration.

Step 2: Integrate API with Stripe

Stripe supports dynamic currency conversion through its API. You will get the unit in the currency local in your webhooks. When you are using unit based measurements you need to convert that local currency back to the unit base you need like USD. Below is a basic integration example using Node.js.

Install Required Packages

npm install stripe axios

Code Example

const stripe = require('stripe')('your-stripe-secret-key');
const axios = require('axios');

// Function to get exchange rates from the API. This will allow easy conversions to units.
async function getExchangeRates(baseCurrency = 'USD') {
	const response = await axios.get(
		`https://spider.cloud/data/exchange-rates`, { headers: { Authorization: `Bearer $REPLACE_WITH_SPIDER_API_KEY` } }
	);
	return response.data.rates;
}

// Create a checkout session with dynamic pricing
async function createCheckoutSession() {
	const rates = await getExchangeRates();

	const session = await stripe.checkout.sessions.create({
		payment_method_types: ['card'],
		line_items: [
			{
				price_data: {
					currency: 'usd', // Change based on customer's location
					product_data: {
						name: 'T-shirt',
					},
					unit_amount: 2000, // e.g., price in USD
				},
				quantity: 1,
			},
		],
		mode: 'payment',
		success_url: 'https://example.com/success',
		cancel_url: 'https://example.com/cancel',
	});

	return session.url;
}

createCheckoutSession()
	.then((url) => console.log('Checkout URL:', url))
	.catch((error) => console.error('Error:', error));

Step 3: Update Frontend

Modify your frontend to display prices in the local currency using the exchange rates obtained from the currency API.

async function displayPrices() {
	const rates = await getExchangeRates();
	const userCurrency = 'EUR'; // This can be determined by user location

	// Assuming product price in USD
	const productPriceUSD = 20.0;

	const productPriceLocal = productPriceUSD * rates[userCurrency];

	document.getElementById('price').innerText = `${productPriceLocal} ${userCurrency}`;
}

displayPrices();

Conclusion

Integrating a currency conversion API with Stripe Checkout can greatly enhance user experience and boost your eCommerce conversions. By following the steps outlined above, you can effortlessly implement local currency pricing, catering to a global customer base and driving higher conversion rates.

For detailed Stripe API documentation and other advanced features, visit the Stripe API reference.

Happy coding!

Build now, scale to millions