实时获取汇率
Ever wanted an accurate and reliable currency data that can be easily integrated into your business; be it CRM, ERP, accounting applications etc., built in any programming languages? Then say hello to currencylayer.
曾经想过可以将其轻松集成到您的业务中的准确可靠的货币数据; 以任何编程语言构建的CRM,ERP,会计应用程序等? 然后向currencylayer打个招呼。
In this article, I will show you how to use two of currencylayer’s services: live exchange rates and historical exchange rates.
在本文中,我将向您展示如何使用currencylayer的两项服务:实时汇率和历史汇率。
什么是currencylayer? ( What is currencylayer? )
Currencylayer provides a reliable exchange rates and currency conversion for your business. It does these by providing an easy to integrate REST API with real-time and historical exchange rates for 168 world currencies and precious metals, delivered in universally usable and easily parseable JSON format, and compatible with any application.
Currencylayer为您的业务提供可靠的汇率和货币换算。 它通过提供一种易于集成的REST API和168种世界货币和贵金属的实时和历史汇率来实现这些目标,它们以通用且易于解析的JSON格式提供,并且与任何应用程序兼容。
The universal real-time exchange rate data solution
通用实时汇率数据解决方案
In addition to providing update to date real-time exchange rates, currencylayer also provides historical exchange rates for all of their supported 168 world currencies. They provide historical exchange rates for way back as the past 16 years.
除了提供最新的实时汇率之外,currencylayer还提供其支持的所有168种世界货币的历史汇率。 他们提供了过去16年的历史汇率。
So what makes currencylayer perfect for your business? Well, here are some reasons:
那么,什么使currencylayer适合您的业务呢? 好吧,这是一些原因:
- Reliable forex rates 可靠的外汇汇率
- Easy-to-integrate API 易于集成的API
- 256-bit HTTPS 256位HTTPS
- Advanced data coverage 先进的数据覆盖
- Real-time updates 实时更新
- Extended usage statistics 扩展使用情况统计
- Easy on your budget 轻松节省预算
- Interactive documentation 互动文件
Did I mention that you can use most of these services completely for free? Sure you can! currencylayer has a free plan. With the free plan, you can make update to 1,000 API requests and the data are updated every hour.
我是否提到您可以完全免费使用其中大多数服务? 你当然可以! currencylayer有免费计划。 使用免费计划,您可以更新到1,000个API请求,并且每小时更新一次数据。
入门 ( Getting started )
To start using the services provided by currencylayer, you need to first create an account with them. So head over to the subscription plans page and signup for a free plan.
要开始使用currencylayer提供的服务,您需要首先使用它们创建一个帐户。 因此,请转到订阅计划页面并注册免费计划。
Once you have signed up, you will be presented with a 3-step quickstart guide. Step 1 contains your Access Key, which you will use to interact with ****the currencylayer API. In step 2, you are presented with the base URL of the currencylayer API:
注册后,将为您提供3个步骤的快速入门指南。 第1步包含您的访问密钥 ,您将使用该访问密钥与currencylayer API进行交互。 在第2步中,向您显示currencylayer API的基本URL:
http://apilayer.net/api/
Take note of your access key and the base URL as we’ll be using them in the next section.
记下您的访问密钥和基本URL,因为我们将在下一部分中使用它们。
获取实时汇率 ( Fetching live exchange rates )
To get live exchange rates, we need to make request to the live
endpoint of the currencylayer API. That is:
要获取实时汇率,我们需要向currencylayer API的live
端点发出请求。 那是:
http://apilayer.net/api/live
We need to pass our access key to the above URL.
我们需要将访问密钥传递到上述URL。
The sample code in this article will be in Node.js and using Axios as the HTTP client:
本文中的示例代码将位于Node.js中,并将Axios用作HTTP客户端:
const axios = require('axios')
const ACCESS_KEY = 'YOUR_ACCESS_KEY'
axios.get(`http://apilayer.net/api/live?access_key=${ACCESS_KEY}¤cies=USD,EUR,GBP,AUD,NGN`).then(response => {
console.log(response.data)
})
Here, we specify we want to get the exchange rates for five currencies: USD
, EUR
, GBP
, AUD
, and NGN
. By default, USD
will be used as the source. That is, the currency to which all exchange rates are relative. We can as well specify as different source:
在这里,我们指定要获取五种货币的汇率: USD
, EUR
, GBP
, AUD
和NGN
。 默认情况下, USD
将用作来源 。 也就是说,所有汇率都是相对的货币。 我们也可以指定不同的来源:
http://apilayer.net/api/live?access_key=YOUR_ACCESS_KEY&source=GBP
Below is the response gotten from the above request:
以下是从上述请求获得的响应:
{
"success": true,
"terms": 'https://currencylayer.com/terms',
"privacy": 'https://currencylayer.com/privacy',
"timestamp": 1526743687,
"source": 'USD',
"quotes": {
"USDUSD": 1,
"USDEUR": 0.849404,
"USDGBP": 0.74245,
"USDAUD": 1.331041,
"USDNGN": 358.000344
}
}
The response is pretty straightforward. The exchange rates are inside the quotes
object.
响应非常简单。 汇率在quotes
对象内。
获取历史汇率 ( Fetching historical exchange rates )
We have seen how to get live exchange rates. Now, let’s see how to fetch exchange rates from the past. For this, we make request to the historical
of the currencylayer API, passing in our access key and a date in the past:
我们已经看到了如何获取实时汇率。 现在,让我们看看如何获取过去的汇率。 为此,我们向currencylayer API的historical
记录发出请求,并传递访问密钥和过去的日期:
const axios = require('axios')
const ACCESS_KEY = 'YOUR_ACCESS_KEY'
axios.get(`http://apilayer.net/api/historical?access_key=${ACCESS_KEY}&date=2010-05-19¤cies=USD,EUR,GBP,AUD,NGN`).then(response => {
console.log(response.data)
})
Here, we specify we want to get the exchange rates for a particular date (2010-05-19) in the past.
在此,我们指定要获取过去特定日期(2010-05-19)的汇率。
Below is the response gotten:
以下是得到的响应:
{
"success": true,
"terms": "https://currencylayer.com/terms",
"privacy": "https://currencylayer.com/privacy",
"historical": true,
"date": "2010-05-19",
"timestamp": 1274313599,
"source": "USD",
"quotes": {
"USDUSD": 1,
"USDEUR": 0.809694,
"USDGBP": 0.69632,
"USDAUD": 1.184999,
"USDNGN": 152.073107
}
}
In addition to the standard result sets, an historical API response contains an historical
key with a value of true
and the specified date
.
除标准结果集外,历史API响应还包含一个具有true
值和指定date
的historical
键。
结论 ( Conclusion )
In this article, we have seen what currencylayer is, the service it provides and some reasons to use it in your business. Though, we only looked at two of the services currencylayer provides. In addition to live and historical exchange rates, currencylayer can also handle currency conversion and lot more.
在本文中,我们了解了什么是currencylayer,它提供的服务以及在您的业务中使用它的一些原因。 不过,我们只研究了currencylayer提供的两项服务。 除了实时汇率和历史汇率外,currencylayer还可以处理货币换算等。
For more information about currencylayer, do check out their website and their easy to understand documentation.
有关currencylayer的更多信息,请查看其网站和易于理解的文档 。
翻译自: https://scotch.io/tutorials/realtime-exchange-rates-with-currencylayer
实时获取汇率