[TC] Gaming API
  • Introduction
  • Getting Started
    • Introduction
      • API Keys
      • Rate Limiting
      • Available APIs
      • Get Support
      • Contribute
  • APIs
    • CityDriving Statistics API
      • Server Status API
      • User Profile API
      • Statistics API
      • Vehicle API
      • AutoMarket API
    • Public Server Status JSON API
    • Discord API
  • Guides
    • Basic Requests
    • Converting LFS Colours
Powered by GitBook
On this page

Was this helpful?

  1. Guides

Basic Requests

PreviousDiscord APINextConverting LFS Colours

Last updated 6 years ago

Was this helpful?

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 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

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)

$.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.')
);

PHP

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

$key = 'YOUR_KEY';
$api = 'citydriving/profile/get';
$params = [
  'key'      => $key,
  'username' => 'USERNAME'
];

$data = TCAPIRequest($api, $params);

// Do something with the returned data
print_r($data);

function TCAPIRequest($cmd, $queryData = null) {
  $opts = ['http' => [
    'method'        => 'GET',
    'timeout'       => 2,
    'ignore_errors' => true
  ]];
  $context = stream_context_create($opts);
  if ($queryData) $queryString = "?" . http_build_query($queryData);
  else $queryString = "";
  $url = 'https://api.tc-gaming.co.uk/' . $cmd . $queryString;
  $result = @file_get_contents($url, false, $context);
  if (!$result) throw new Exception('Query failed.');
  else {
    $ret = json_decode($result, true);
    if (json_last_error() > 0) throw new Exception($result);
    return $ret;
  }
}

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

Try out this example on

User Profile API
JS Bin
JQuery
JS Bin