Visual Basic 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.

 Imports System.Threading
 Imports Newtonsoft.Json
 Imports RestSharp
 
 Module Module1
    Const API_KEY_ID = "" ' get your own
    Const USERNAME = "" ' put your username here;
    Const PASSWORD = "" ' put your password here;
    Dim client As RestClient
    Dim session_id As String
    Dim user_id As String

    Sub Main()
        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(Environment.NewLine)
        Console.WriteLine("Fetching My Account Info...")
        Dim user = FetchMyAccountInfo()
        Console.WriteLine("Display Name: " + user("display_name"))
        Console.WriteLine("email:        " + user("email"))
        Thread.Sleep(1000)

        Console.WriteLine(Environment.NewLine)
        Console.WriteLine("Uploading A File...")
        Dim 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(Environment.NewLine)
        Console.WriteLine("Searching Games...")
        Dim games = SearchGames()
        For Each game As Object In games
            Console.WriteLine("Game Name:    " + game("name"))
            Console.WriteLine("Game ID:      " + game("id"))
            Thread.Sleep(100)
        Next

        Console.WriteLine(Environment.NewLine)
        Console.Write("Hit any key to close...")
        Console.ReadKey()
    End Sub

    Sub GetASession(username As String, password As String)
        Dim request = New RestRequest("session", Method.POST)
        request.AddParameter("username", username)
        request.AddParameter("password", password)
        request.AddParameter("api_key_id", API_KEY_ID)

        Dim response = client.Execute(request)
        Dim data = JsonConvert.DeserializeObject(response.Content)

        session_id = data("result")("id")
        user_id = data("result")("user_id")
    End Sub

    Function FetchMyAccountInfo() As Object
        Dim request = New RestRequest("user/" + user_id, Method.GET)
        request.AddParameter("session_id", session_id)

        Dim response = client.Execute(request)
        Dim data = JsonConvert.DeserializeObject(response.Content)

        Dim user = data("result")

        Return user
    End Function

    Function UploadAFile(user As Object, filepath As String, filename As String) As Object
        Dim 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)

        Dim response = client.Execute(request)
        Dim data = JsonConvert.DeserializeObject(response.Content)
        Dim file = data("result")

        Return file
    End Function

    Function SearchGames() As Object
        Dim request = New RestRequest("game", Method.GET)
        request.AddParameter("q", "Steampunk")
        request.AddParameter("session_id", session_id)

        Dim response = client.Execute(request)
        Dim data = JsonConvert.DeserializeObject(response.Content)

        Dim games = data("result")("items")

        Return games
    End Function
 End Module
^ Back to Top ^