Welcome to the UnirateAPI Perl documentation! Our free exchange rate API provides real-time currency conversion capabilities that you can easily integrate into your Perl applications. Whether you're building a web service, data processing script, or any other Perl application that needs currency conversion, our API has you covered.
UnirateAPI is designed to be developer-friendly and reliable, with a focus on simplicity and performance. This guide will show you how to make API requests using Perl and popular CPAN modules.
For more detailed information about our API endpoints, rate limits, and advanced features, please check our main API documentation.
Below you'll find examples showing how to use our API with Perl.
You can use our API in Perl by making HTTP requests using popular modules like LWP::UserAgent, HTTP::Tiny, or Mojo::UserAgent. We'll show you examples using different approaches.
# Option 1: Using LWP::UserAgent (standard Perl HTTP client)
use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Request::Common qw(GET);
use URI;
use JSON qw(decode_json);
use Data::Dumper;
# Create a simple UnirateAPI client class
package UnirateAPI;
sub new {
my ($class, $api_key) = @_;
die "API key is required" unless $api_key;
my $self = {
api_key => $api_key,
base_url => 'https://api.unirateapi.com/api',
ua => LWP::UserAgent->new(
timeout => 10,
agent => 'Perl UnirateAPI Client/1.0',
),
};
return bless $self, $class;
}
sub convert {
my ($self, $from_currency, $to_currency, $amount) = @_;
my $uri = URI->new($self->{base_url} . '/convert');
$uri->query_form(
api_key => $self->{api_key},
from => $from_currency,
to => $to_currency,
amount => $amount,
);
my $response = $self->{ua}->get($uri);
if ($response->is_success) {
return decode_json($response->decoded_content);
} else {
die "API request failed: " . $response->status_line;
}
}
sub get_rate {
my ($self, $from_currency, $to_currency) = @_;
my $uri = URI->new($self->{base_url} . '/rates');
$uri->query_form(
api_key => $self->{api_key},
base => $from_currency,
symbols => $to_currency,
);
my $response = $self->{ua}->get($uri);
if ($response->is_success) {
my $data = decode_json($response->decoded_content);
return $data->{rates}->{$to_currency};
} else {
die "API request failed: " . $response->status_line;
}
}
sub get_currencies {
my ($self) = @_;
my $uri = URI->new($self->{base_url} . '/currencies');
$uri->query_form(
api_key => $self->{api_key},
);
my $response = $self->{ua}->get($uri);
if ($response->is_success) {
return decode_json($response->decoded_content);
} else {
die "API request failed: " . $response->status_line;
}
}
package main;
# Example usage
my $api_key = 'YOUR_API_KEY';
my $api = UnirateAPI->new($api_key);
eval {
# Convert 100 USD to EUR
my $conversion = $api->convert('USD', 'EUR', 100);
print "Converted amount: $conversion->{result} $conversion->{to}\n";
# Get exchange rate from USD to EUR
my $rate = $api->get_rate('USD', 'EUR');
print "1 USD = $rate EUR\n";
# Get supported currencies
my $currencies = $api->get_currencies();
print "Supported currencies: ";
my $count = 0;
foreach my $code (sort keys %$currencies) {
print "$code ";
$count++;
last if $count >= 5;
}
print "...\n";
};
if ($@) {
print "Error: $@\n";
}
# Option 2: Using HTTP::Tiny (lightweight HTTP client)
use strict;
use warnings;
use HTTP::Tiny;
use URI;
use URI::Escape;
use JSON qw(decode_json);
my $api_key = 'YOUR_API_KEY';
my $base_url = 'https://api.unirateapi.com/api';
my $http = HTTP::Tiny->new(timeout => 10);
# Function to make API request
sub make_request {
my ($endpoint, $params) = @_;
# Build query string
my $uri = URI->new($base_url . $endpoint);
$uri->query_form(%$params);
# Make the request
my $response = $http->get($uri);
if ($response->{success}) {
return decode_json($response->{content});
} else {
die "API request failed: $response->{status} $response->{reason}";
}
}
# Example of converting currency
eval {
my $conversion = make_request('/convert', {
api_key => $api_key,
from => 'USD',
to => 'EUR',
amount => 100,
});
print "Using HTTP::Tiny: $conversion->{amount} $conversion->{from} = $conversion->{result} $conversion->{to}\n";
};
if ($@) {
print "Error: $@\n";
}
# Option 3: Using Mojolicious (modern web framework with built-in UserAgent)
# First, install Mojolicious: cpanm Mojolicious
# This is typically run with: perl script.pl
use strict;
use warnings;
use Mojo::UserAgent;
use Mojo::JSON qw(decode_json);
use Mojo::URL;
# Set up Mojo::UserAgent
my $ua = Mojo::UserAgent->new;
$ua->max_redirects(5);
$ua->connect_timeout(5);
my $api_key = 'YOUR_API_KEY';
my $base_url = 'https://api.unirateapi.com/api';
# Convert currency with Mojo
sub convert_mojo {
my ($from, $to, $amount) = @_;
my $url = Mojo::URL->new($base_url . '/convert');
$url->query(
api_key => $api_key,
from => $from,
to => $to,
amount => $amount,
);
my $tx = $ua->get($url);
if ($tx->res->is_success) {
return $tx->res->json;
} else {
die "API request failed: " . $tx->res->message;
}
}
# Example with Mojolicious
eval {
my $result = convert_mojo('USD', 'EUR', 100);
print "Using Mojolicious: $result->{amount} $result->{from} = $result->{result} $result->{to}\n";
};
if ($@) {
print "Error: $@\n";
}
# API Response:
{
"from": "USD",
"to": "EUR",
"amount": 100,
"result": 92.45
}
# Console Output:
Converted amount: 92.45 EUR
1 USD = 0.9245 EUR
Supported currencies: USD EUR GBP JPY AUD ...
Sign up now to get your API key and start converting currencies in your perl applications.
Get Your API Key