Your First Kotlin REST API Client

Rahul Khanna
3 min readSep 20, 2024

--

If you are new to Kotlin or want to fetch data from an API for your app for Android or any other JVM-based platform you are at the right place.

In this guide, we will walk you through the basics of getting market data into your application which will go a long way in using external data sources to bring your apps to life.

Let’s Begin!

In this tutorial, we will use the TraderMade JVM client but any other can be used to make data calls and get live and historical data into your apps.

Installation

To integrate the JVM Client into your Gradle project, you first need to add the repository.

dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}

Next, include the dependency in your project by following the line to your grade file.

dependencies {
implementation 'com.github.tradermade:client-tms-jvm:1.0.4'
}

Please see the GitHub releases for the current release version.

Importing the JVM Client

package io.tradermade.test_client_jvm

Getting Started with TraderMade’s API

Before you start making API calls, you’ll need to create an account on TraderMade. Signing up is free, and once you’re signed in, you can generate your API key by visiting the dashboard.

With your API key in hand, you can now initialize the TraderMade API client in Kotlin

val api = TraderMadeAPI("YOUR_API_KEY")

Replace "YOUR_API_KEY" with your actual API key, and you're ready to make your first API requests!

Once the API key is set simply request data using the getLiveData function that gets you a JSON object with the latest market quotes you can display on your app and refresh as and when needed.

val liveData = api.getLiveData("EURUSD,GBPUSD")

If you want to create a table or graph of how the market has moved during the day, week, or month, you can request historical data providing a currency pair and date. The response will be a JSON object with OHLC data.

val historicalData = api.getHistoricalData("EURUSD", "2023-08-01")

Similarly, if you want to draw a EURUSD candlestick chart you can request time series data that gets you daily, hourly, and minute data.

val timeSeriesData = api.getTimeSeriesData("EURUSD", "2023-08-01", "2023-08-10", "daily", "1")

Important notes:

Daily data: Access up to 15 years of data (maximum of 1 year per request).

Hourly data: Access up to 12 months of data (maximum of 1 month per request).

Minute data: Access up to 1 month of data (maximum of 2 days per request).

Finally we can also get the Conversion rate to build a currency converter app using the convertCurrency endpoint.

val convertedAmount = api.convertCurrency("EUR", "USD", 1000.0)

This request converts 1,000 EUR to USD at the current exchange rate.

The TraderMade JVM Client makes it easy to integrate real-time and historical market data into your Kotlin applications. With just a few lines of code, you can access live Forex data, and historical data, perform currency conversions, and much more.

In the next tutorial, we will see how to implement this in an app. Feel free to use our sample code for the Android app.

For more information and to explore additional examples, check out the GitHub repository and the official TraderMade API documentation.

If you like similar articles please subscribe, comment, and clap our tutorials it goes a long way in producing such articles.

--

--

Rahul Khanna
Rahul Khanna

Written by Rahul Khanna

Chief Technology Officer (CTO), TraderMade

No responses yet