Skip to main content

Quick Start

Develop and test your game's API calls in Unity without waiting for backend services.


Video​


The Problem​

Backend's behind? Network unstable? Want to work offline? Need to test errors?

  • Backend’s not ready → define the API contract yourself, build now, switch to real backend later
  • Need to test errors → configure success/error/timeout/invalid responses on demand
  • Want offline dev → run the game without network, no VPN needed
  • Testing pagination → cycle through different responses automatically

Route API calls to:

  • Real backends (production, staging)
  • QA servers
  • Localhost
  • Mock responses (custom status codes, latency, data)

API Mocking Toolkit Routing


Installation​

  1. Open Unity Asset Store
  2. Search "API Mocking Toolkit" → Import
  3. Import all files

Requirements: Unity 2021.3+, no external dependencies


Demo Scene​

1. Open demo sceneAssets > CodeCarnage > ApiMockingToolkit > Samples > DemoScene > DemoScene.unity
2. Press PlayScene loads with two buttons: Get Users and Get Posts
3. Click "Get Users"Sends request → Toolkit intercepts → Returns mock data instantly, no internet needed
4. Click "Get Posts" multiple timesCycles through pages 1 → 2 → 3 → 1 (Response Strategies in action)

How It Works​

Open DemoController.cs:

public async void OnGetUsersClicked()
{
var response = await ApiClient.Get("{{baseUrl}}/users");
DisplayResponse(response);
}

Standard API call. {{baseUrl}} resolves per active environment. Toolkit intercepts matching URLs and returns your mock data. No if (testing) checks. Same code works with mocks or real backends.


Create Your First Endpoint​

Mock your game's API using JSONPlaceholder's /comments endpoint as a stand-in. Works offline with mocks, or online hitting the real API.

1. Open toolkit windowWindow > CodeCarnage > API Mocking Toolkit
2. Create endpoint

Click "+ Endpoint", set:

3. Add mock response

Status Code: 201

{
"id": 501,
"postId": 1,
"name": "Demo comment from Unity",
"email": "player@example.com",
"body": "Returned from mock"
}
4. Enable Offline ModeToggle ON at the top
5. Test
using System.Collections.Generic;
using UnityEngine;
using CodeCarnage.ApiMockingToolkit;

public class ProfileTest : MonoBehaviour
{
async void Start()
{
var requestBody = "{
"postId": 1,
"name": "Demo comment",
"email": "player@example.com"
}";
var headers = new Dictionary<string, string> { { "Content-Type", "application/json" } };
var response = await ApiClient.Post("https://jsonplaceholder.typicode.com/comments", requestBody, headers);
Debug.Log($"Status: {response.StatusCode}");
}
}

Next Steps​


Troubleshooting​

Nothing happening? Make sure Offline Mode is ON, URL matches exactly (case-sensitive), check Console for errors.

Demo scene not working? Check "Demo Scene Collection" is selected, re-import Samples from Package Manager.

Questions? Check Troubleshooting FAQ or email support@codecarnage.com