C# Example

NOTE: Get an APIKey.

This code will allow you to get a session, fetch account info, upload a file, and search games. From it you should be able to discern how to work with the entire TGC API.

 using System;
 using System.Threading;
 using Newtonsoft.Json;
 using RestSharp;
 
 namespace TGCTutorial
 {
     class Program
     {
         const string API_KEY_ID = ""; // get your own
         const string USERNAME = ""; // put your username here;
         const string PASSWORD = ""; // put your password here;
         static RestClient client;
         static string session_id;
         static string user_id;
 
         static void Main(string[] args)
         {
             client = new RestClient("https://www.thegamecrafter.com/api/");
 
             Console.WriteLine("Getting A Session...");
             GetASession(USERNAME, PASSWORD);
             Console.WriteLine("Session ID:   " + session_id);
             Console.WriteLine("User ID:      " + user_id);
             Thread.Sleep(1000);
 
             Console.WriteLine("\n");
             Console.WriteLine("Fetching My Account Info...");
             dynamic user = FetchMyAccountInfo();
             Console.WriteLine("Display Name: " + user.display_name);
             Console.WriteLine("email:        " + user.email);
             Thread.Sleep(1000);
 
             Console.WriteLine("\n");
             Console.WriteLine("Uploading A File...");
             dynamic file = UploadAFile(user, "path/to/image.png", "image.png");
             Console.WriteLine("File name:    " + file.name);
             Console.WriteLine("File ID:      " + file.id);
             Thread.Sleep(1000);
 
             Console.WriteLine("\n");
             Console.WriteLine("Searching Games...");
             dynamic games = SearchGames();
             foreach (dynamic game in games)
             {
                 Console.WriteLine("Game Name:    " + game.name);
                 Console.WriteLine("Game ID:      " + game.id);
                 Thread.Sleep(100);
             }
 
             Console.WriteLine("\n");
             Console.Write("Hit any key to close...");
             Console.ReadKey();
         }
 
         static void GetASession(string username, string password)
         {
             RestRequest request = new RestRequest("session", Method.POST);
             request.AddParameter("username", username);
             request.AddParameter("password", password);
             request.AddParameter("api_key_id", API_KEY_ID);
 
             var response = client.Execute(request);
             dynamic data = JsonConvert.DeserializeObject(response.Content);
 
             session_id = data.result.id;
             user_id = data.result.user_id;
         }
 
         static dynamic FetchMyAccountInfo()
         {
             RestRequest request = new RestRequest("user/" + user_id, Method.GET);
             request.AddParameter("session_id", session_id);
 
             var response = client.Execute(request);
             dynamic data = JsonConvert.DeserializeObject(response.Content);
 
             dynamic user = data.result;
 
             return user;
         }
 
         static dynamic UploadAFile(dynamic user, string filepath, string filename)
         {
             RestRequest request = new RestRequest("file", Method.POST);
             request.AddFile("file", filepath);
             request.AddParameter("name", filename);
             request.AddParameter("folder_id", user.root_folder_id);
             request.AddParameter("session_id", session_id);
 
             var response = client.Execute(request);
             dynamic data = JsonConvert.DeserializeObject(response.Content);
             var file = data.result;
 
             return file;
         }
 
         static dynamic SearchGames()
         {
             RestRequest request = new RestRequest("game", Method.GET);
             request.AddParameter("q", "Steampunk");
             request.AddParameter("session_id", session_id);
 
             var response = client.Execute(request);
             dynamic data = JsonConvert.DeserializeObject(response.Content);
 
             dynamic games = data.result.items;
 
             return games;
         }
     }
 }
^ Back to Top ^