Sample Code & Wrappers


C#

Wrapper

Run the following command in the Package Manager Console:

PM> Install-Package CSharpExchangeRatesAPI

Sample Code

Here is a minimalistic example to access the API without the API wrapper:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System;
using System.Web.Script.Serialization;

using System.Collections.Generic;

using System.IO;
using System.Net;


public class ExampleAPI
{
    public static void Main(string[] args)
    {
        try
        {
            var url = "https://web-services.oanda.com/rates/api/v1/currencies.json";
            var request = (HttpWebRequest)WebRequest.Create(url);

            string json = "";
            string credentialHeader = String.Format("Bearer <YOUR_API_KEY_HERE>");
            request.Method = "GET";
            request.ContentType = "application/json";
            request.Headers.Add("Authorization", credentialHeader);

            HttpWebResponse webresponse = (HttpWebResponse)request.GetResponse();

            var sw = new StreamReader(webresponse.GetResponseStream(), System.Text.Encoding.ASCII);
            json = sw.ReadToEnd();
            sw.Close();

            var response = (new JavaScriptSerializer()).Deserialize<Dictionary<string, List<object>>>(json);

            foreach (Dictionary<string,object> currency in response["currencies"])
            {
                Console.WriteLine("{0} : {1}", currency["code"], currency["description"]);
            }
        }
        catch(Exception e)
        {
            Console.WriteLine("Exception: " + e.Message);
        }
    }
}

Java

Wrapper


Node.js

Wrapper

npm install oanda-exchange-rates --save

Sample Code

Here is a minimalistic example to access the API without the API wrapper:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var https = require('https');

https.get({
   hostname: 'web-services.oanda.com',
   path: '/rates/api/v1/rates/USD.json?date=2014-01-01&quote=EUR',
   headers: {
      Authorization: 'Bearer ' + MY_API_KEY
   }
}, function(response) {
      if (response.statusCode === 200) {
          response.on('data', function(data) {
              console.log(JSON.parse(data));
          });
      }
});

Perl

Wrapper

With cpanm:

cpanm WebService::OANDA::ExchangeRates

With cpan:

cpan WebService::OANDA::ExchangeRates

Manual Build:

As a last resort, you can manually install it. Download the tarball, untar it,
then build it:

    % perl Makefile.PL
    % make && make test

Then install it:

    % make install

Sample Code

If you wish to develop your own code to access the API, here is a simple example using the LWP::UserAgent Module:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
use strict;
use warnings;

use Data::Dumper;
use HTTP::Headers;
use LWP::UserAgent;
use JSON::XS;

my $ua = LWP::UserAgent->new(
    verify_hostname => 0,
    default_headers => HTTP::Headers->new(
        Authorization => sprintf 'Bearer %s', 'MY_API_KEY'
    );

);

my $response = $ua->get(
    'https://web-services.oanda.com/rates/api/v1/rates/USD.json?date=2014-01-01&quote=EUR'
);
if ($response->is_success) {
    print Dumper JSON::XS::decode_json( $response->content );
}

SPORE

SPORE is a method of defining an API using a JSON specification. See the Implementations section on Github for information on generating a client. As of this writing, there are SPORE clients for:

  • Clojure
  • Javascript
  • Lua
  • Nodejs
  • Perl
  • Python
  • Ruby

The URL for use with a SPORE client is: https://web-services.oanda.com/rates/api/v1/spore