FIRST STEPS WITH XAPI, C# AND VISUAL STUDIO

This tutorial shows how to combine xAPI, C# and Visual Studio to create wonderful financial applications. It describes some basic concepts on how to use the .NET wrapper.

Commands used in this tutorial:

  • login,
  • getServerTime,
  • getAllSymbols.

Before you start...

Step 1. Create new project

  1. Extract the downloaded archive and open <wrappers folder>/xAPIConnector.sln file
  2. Click Solution xAPIConnector with right mouse button and choose Add > New Project... option
  3. Choose Visual C# > Console Application, enter the name for your project and click OK

Step 2. Add references in your project

  1. In your new project tree click References with right mouse button and choose Add Reference...
  2. Choose Solution > Projects from the menu on the left, select xAPI and click OK

Step 3. Set your project as a startup project

  1. Click on your new project with right mouse button and choose Set as StartUp Project option

Wrappers are ready now so we can create our first application!

Step 4. Write the code

  1. Expand your project and double click the Program.cs class
  2. Add necessary imports at the beginning of the file
  3. using xAPI;
    using xAPI.Codes;
    using xAPI.Connection;
    using xAPI.Commands;
    using xAPI.Errors;
    using xAPI.Records;
    using xAPI.Responses;
    using xAPI.Utils;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
  4. Write the following code in the main function (between '{' and '}' brackets)
  5. SpecificServer server = Servers.DEMO;
    string userId = "12345";
    string password = "password";
    string appName = "firstApp";
    // should be left blank
    // to get more info about appId and appName visit http://developers.xstore.pro/api/tutorials/appid_and_appname
    string appId = "";
    try
    {
        // Connect to server
        APISync apiClient = new APISync(server);
        Console.WriteLine("Connected to the server");
        // Login to server
        Credentials credentials = new Credentials(userId, password, appId, appName);
        LoginResponse loginResponse = apiClient.Login(credentials);
        Console.WriteLine("Logged in as: " + userId);
        // Execute GetServerTime command
        GetServerTimeResponse serverTimeResponse = apiClient.GetServerTime();
        Console.WriteLine("Server time: " + serverTimeResponse.TimeString);
        // Execute GetAllSymbols command
        GetAllSymbolsResponse allSymbolsResponse = apiClient.GetAllSymbols();
        Console.WriteLine("All symbols count: " + allSymbolsResponse.SymbolRecords.Count);
        // Print first 5 symbols
        Console.WriteLine("First five symbols:");
        foreach (SymbolRecord symbolRecord in allSymbolsResponse.SymbolRecords.Take(5))
        {
            Console.WriteLine(" > " + symbolRecord.Symbol + " ask: " + symbolRecord.Ask + " bid: " + symbolRecord.Bid);
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("An exception occured: " + ex.ToString());
    }
    Console.Read();
  6. Please remember to change your credentials in the first lines of the code above

Step 5. Run the application

  1. Locate and click the green triangle icon on the top toolbar
  2. As a result you should see your application running like on the screen below

Summary

In this tutorial you learned how to prepare Visual Studio to work with xAPI and .NET wrappers. You have also created a very simple application using C# that:

  • connects to the xAPI,
  • logins to the server,
  • gets server time,
  • gets list of all available symbols,
  • lists first five of them.

Now you are ready to create your own app!