CFDs are complex instruments and come with a high risk of losing money rapidly due to leverage. 64 % of retail investor accounts lose money when trading CFDs with this provider. You should consider whether you understand how CFDs work and whether you can afford to take the high risk of losing your money.

I need an example of a basic program in Java for the Darwin API

Hola a todos,

Llevo metido como inversor en Darwinex casi dos años. Me ha ido bastante mal, pero no me desanimo, espero que cada error haya sido sólo una lección aprendida. Ahora estoy intentando montarme un programa en Java para poder consumir el API ya que tengo conocimientos de programación y creo que puedo montar algo interesante que me ayude.
El problema es que me está costando horrores arrancar, solo necesito un ejemplo de programa Java para consumir el APIDarwin con algunas de las funcionalidades que tiene. Una vez que tenga ese ejemplo y me funcione, puedo tirar yo y hacer una aplicación interesante poco a poco.

Un saludo y muchas gracias por adelantado.

Hola Hector, nadie te va a poder ayudar si no lo escribes en inglés (esta comunidad es SOLO en inglés).
Hi Hector, please repost your comment in Englsh, so maybe someone can help you (this is an English community).

3 Likes

Hi @HectorGM2,

Let’s see if I understood the question here to (“I need an example of a basic program in Java for the Darwin API”) …

There are many ways to use our API using Java. You could use plain Java like in this example:

package com.darwinex.recruiting;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class GetProductsExample {

    private final static String ACCESS_TOKEN = "<access-token>";

    public static void main(String[] args) {
        try {
            URL url = new URL("https://api.darwinex.com/darwininfo/products");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Accept", "application/json");
            conn.setRequestProperty("Authorization", "Bearer " + ACCESS_TOKEN);
            if (conn.getResponseCode() != 200) {
                throw new RuntimeException("Failed with HTTP error code : " + conn.getResponseCode());
            }

            BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
            System.out.println("Products Received:\n" + br.readLine());
            conn.disconnect();
        } catch (final IOException e) {
            e.printStackTrace();
        }
    }
}

Or you could use any existing Java framework to make it easier, like Spring, Micronaut, Vert.X, Quarkus … There are many examples and tutorials of REST clients with Java in internet. It might get a bit more complex for developing a proper trading bot but for the Darwin Info API it is as simple as calling our API providing the access token in the authorization header.

Another way to use the API could be to use autogenerated clients of the API. In our API Store you might find the swagger specification in every API:


The swagger specification is a machine readable specification of the API. Using tools like swagger-codegen ( https://swagger.io/tools/swagger-codegen/ ) you can get an autogenerated client providing this swagger.
Example of code using autogenerated client:

ApiClient apiClient = new ApiClient();
apiClient.setBasePath("https://api.darwinex.com/darwininfo/1.5");
apiClient.addDefaultHeader("Authorization", "Bearer " + ACCESS_TOKEN);
PageProductDTO products = api.productsGet("ACTIVE", 0, 50);
products.getContent().forEach(p -> System.out.println(p.getProductName()));
3 Likes

Thank you very much, it’s just what I needed. Now with this base I will try to make a heavy JAVA application to which I will gradually add functionality.

1 Like

I have been lucky, but I keep that in mind next time.

1 Like