Trading Economics API Authentication

To access the API, subscribe to a plan and get your API key at developer.tradingeconomics.com. You can pass the key as a c or client URL parameter, or using an Authorization HTTP header:

Authentication

import requests
api_key = 'YOUR_API_KEY'
url = f'https://api.tradingeconomics.com/country/mexico?c={api_key}'
data = requests.get(url).json()
print(data)

The response data format can be configured by appending the &f= parameter to the URL request (supported formats: JSON, CSV, XML)

url = f'https://api.tradingeconomics.com/country/mexico?c={api_key}&f=xml'

You can use request headers to pass the API key:

import requests

response = requests.get('https://api.tradingeconomics.com/country/mexico', headers = {'Authorization': 'api_key'})
print(response.json())
const axios = require('axios');
(async () => {    
    const api_key = 'YOUR_API_KEY'
    const response = await axios.get(`https://api.tradingeconomics.com/country/mexico?c=${api_key}`)
    console.log(response.data)
})()

The response data format can be configured by appending the &f= parameter to the URL request (supported formats: JSON, CSV, XML)

const response = await axios.get(`https://api.tradingeconomics.com/country/mexico?c=${api_key}&f=xml`)

You can use request headers to pass the API key:

(async () => {
  const url = 'https://api.tradingeconomics.com/country/mexico';
  const headers = { 'Authorization': 'api_key' };

  try {
    const response = await fetch(url, { method: 'GET', headers });
    const data = await response.text();
    console.log(data);
  } catch (error) {
    console.error(error);
  }
})();

To use Trading Economics API with any server based language, copy and paste an endpoint and change its parameters to what you like.

By default, Server based languages show data as string:

using (var httpClient = new HttpClient())
{
    using (var request = new HttpRequestMessage(new HttpMethod("GET"), "https://api.tradingeconomics.com/country/mexico?c=your_api_key"))
    {
        request.Headers.TryAddWithoutValidation("Upgrade-Insecure-Requests", "1");
        var response = await httpClient.SendAsync(request);
        if (response.IsSuccessStatusCode)
        {
            var content = await response.Content.ReadAsStringAsync(); 
            Console.WriteLine(content);
        }
    }
}

The response data format can be configured by appending the &f= parameter to the URL request (supported formats: JSON, CSV, XML)

new HttpRequestMessage(new HttpMethod("GET"), "https://api.tradingeconomics.com/country/mexico?c=your_api_key&f=xml")

Packages

You can also use Trading Economics packages to have easy access to API. Below, we present a basic example to illustrate how to use these packages:

Pre-requirements

Install the tradingeconomics package using pip, a package management system used to install and manage software packages written in Python. In Windows Command Prompt or Linux bash type:

pip install tradingeconomics

Using package

import tradingeconomics as te                #1
te.login('your_api_key')                     #2
data = te.getIndicatorData(country='mexico') #3
  • #1 Imports Trading Economics Python package

  • #2 Calls the authentication method

  • #3 Makes a call to the API. You can change the parameters to what you like

By default, the response will be a list of dictionaries.

You can also change the output type to Pandas dataframe:

data = te.getIndicatorData(country='mexico', output_type='df')

Pre-requirements

Install the tradingeconomics package using npm, a package management system used to install and manage software packages written in JavaScript. In Windows Command Prompt or Linux bash type:

npm install tradingeconomics

Using package

const te = require('tradingeconomics');                              //1
te.login('your_api_key');                                            //2
data = te.getIndicatorData(country = 'mexico').then(function(data){  //3
});
  • //1 Imports Trading Economics Node.js package

  • //2 Calls the authentication method

  • //3 Makes a call to the API. You can change the parameters to what you like

By default, Node.js package shows data as array of Javascript objects.