Basic Requests

If you're new to using REST APIs, this guide should help you get started with making basic requests. It provides reference implementations for fetching data from our API by using the User Profile API as an example.

Our code examples are available in JavaScript and PHP (change language in the top right).

Reference Implementations

JavaScript

Here is our reference implementation for fetching data with JavaScript. You will need to change the values of YOUR_KEY and USERNAME.

Try out this example on JS Bin

var key = 'YOUR_KEY';
var request = {
  api: 'citydriving/profile/get',
  params: {
    key: key,
    username: 'USERNAME'
  }
};

TCAPIRequest(request, function(data) {

  // Do something with the returned data
  console.log(data);
});

function TCAPIRequest(req, callback) {
  this.baseUrl = 'https://api.tc-gaming.co.uk/';
  this.xhr = new XMLHttpRequest();
  this.qs = "";
  if(Object.keys(req.params).length > 0) {
    for(var param in req.params) {
      this.qs.length == 0 ? (qs += "?") : (qs += "&");
      this.qs += param + '=' + req.params[param];
    }
  }
  this.url = this.baseUrl + req.api + this.qs;
  this.xhr.onreadystatechange = function() {
    if(this.xhr.readyState === XMLHttpRequest.DONE) {
      if(this.xhr.status === 200) callback(JSON.parse(this.xhr.response));
    }
  }.bind(this);
  this.xhr.open('GET', this.url);
  this.xhr.send();
}

JavaScript (JQuery)

If you would prefer, here is our reference implementation for fetching data with JQuery. You will need to change the values of YOUR_KEY and USERNAME.

Try out this example on JS Bin

$.get(
  "https://api.tc-gaming.co.uk/citydriving/profile/get",
  {
    key: 'YOUR_KEY',
    username: 'USERNAME'
  }
).done(
  function(data) {

    // Do something with the returned data
    console.log(data);
  }
).fail(
  console.log('Error fetching data!\nMake sure you have set your API key and parameters.')
);

Last updated