WTO API loop by country

WTO API offers a large data download limit (up to 1M obs.) once you registered an account for free. Below you can try with my old API token (no guarantee on it continues to work).

Changes from original version:

  • Added API token to max out the download limit

  • Replace the loop by reporting countries rather than year

python:
import json
import numpy        as  np
import pandas       as  pd
import requests
api_key = 'bdc1a5cb69ef4b6f9c444fecc2778d19'

def WTO_Scraper(r  : int,
                key: str,
                m  : str= 'codes',
                pc : str=   'HS2',
                y0 : int=    2009,
                y1 : int=    2019,
                obs: int= 1000000):
    """
    Wrapper for creating URLs to access the WTO API

    ARGUMENTS
    *********
    Required
      r   = Country Code
      key = User API Key

    Optional
      m   = Mode                                default= 'codes'
      pc  = Product Code                        default=   'HS2'
      y0  = Year (Start)                        default=    2009
      y1  = Year (End)                          default=    2019
      obs = Maximum Observations per Call       default= 1000000
    """

    base      = 'https://api.wto.org/timeseries/v1/data?i=HS_M_0010'
    r         = str(r).zfill(3)
    url       = f'{base}&r={r}&pc={pc}&ps={y0}-{y1}&mode={m}&max={obs}&subscription-key={key}'

    result    = requests.get(url).json()
    if 'Dataset' in result: 
        df        = pd.DataFrame(result['Dataset'])
        df        = df.replace({None: np.nan})
        df.columns= [i[:32] for i in df.columns]

        df.to_stata(f'HS2_{r}.dta')
        return df

for i in range(000,999): WTO_Scraper(i, api_key)
end

di "`c(pwd)'" // Display path to current folder
local files : dir "`c(pwd)'" files "*.dta" 
foreach x of local files {
    di "`x'" // Display file name
	append using `x'
}