> ## Documentation Index
> Fetch the complete documentation index at: https://docs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# XSAPI C overview

> Introduction to the XSAPI C API layer for XBOX services in GDK titles, covering memory allocation callbacks, threading control, and libHttpClient.

The XBOX services API (XSAPI) C API is the XSAPI layer that's included in the GDK for games on PC and XBOX consoles. To set up XSAPI in your game, see [How to use XBOX services APIs](https://docs.xbox.com/services/xbox-services/fundamentals/xbox-services-api/live-gs-xbl-apis.md).

## XSAPI C libraries

The **Library** line on each XSAPI C reference page names the static libraries. The GDK provides these XSAPI C libraries:

| Library | Use |
| - | - |
| `Microsoft.Xbox.Services.143.C.lib` | Static library for the v143 and later toolsets. Available for x64 (PC and XBOX consoles) and ARM64 (PC). |
| `Microsoft.Xbox.Services.142.C.lib` | Static library for the v142 toolset. Available for x64 only. |
| `Microsoft.Xbox.Services.C.Thunks.lib` | Import library for the XSAPI DLL. Use it when more than one DLL in your game calls XSAPI, so that all of them share one XSAPI instance. |

Each library also has a `.Debug` variant. When you link a static library, also link `libHttpClient.lib`, the import library for `libHttpClient.dll`. Copy `libHttpClient.dll`, and `Microsoft.Xbox.Services.C.Thunks.dll` if you use the DLL, from the GDK `bin` folder into your game's layout. For the full list of libraries, see [GDK directory structure](https://docs.xbox.com/build/gdk-and-engines/guides/gdk-directory-structure.md).

## Benefits of the C API for XSAPI

* Allows titles to control the memory allocations when calling XSAPI.
* Allows titles to gain full control of thread handling when calling XSAPI.
* Uses a new HTTP library, libHttpClient, designed for game developers.

You can use the C APIs alongside C++ XSAPI, but you will not gain the previously listed benefits with the C++ APIs.

### Managing memory allocations

With the C API, you can now specify a function callback that XSAPI will call whenever it tries to allocate memory.
If you do not specify function callbacks, XSAPI will use standard memory allocation routines.

To manually specify your memory routines, you can do the following:

* At the start of the game:
  * Call `XblMemSetFunctions(memAllocFunc, memFreeFunc)` to specify the allocation callbacks for assigning and releasing memory.
  * Call `XblInitialize()` to initialize the library instance.
* While the game is running:
  * Calling any of the new C APIs in XSAPI that allocate or free memory will cause XSAPI to call the specified memory handling callbacks.
* When the game exits:
  * Wait for your outstanding XSAPI calls to finish, and then call [XblCleanupAsync](https://docs.xbox.com/reference/live/xsapi-c/xbox_live_global_c/functions/xblcleanupasync.md) and wait for it to finish. This reclaims all resources associated with the XSAPI library.
  * Clean up your game's custom memory manager.

### Managing asynchronous threads

The C API introduces a new asynchronous thread calling pattern that allows developers full control over the threading model.
For more information, see [Calling pattern for XSAPI C layer async calls](https://docs.xbox.com/services/xbox-services/fundamentals/xbox-services-api/live-flatc-async-patterns.md).

## Migrating code to use C XSAPI

The XSAPI C APIs can be used alongside the XSAPI C++ APIs in a project, so we recommend that you migrate one feature at a time.

The C APIs and C++ APIs are really just thin wrappers around a common core, just with different entry points, so the functionality should be unchanged.

However, only the C APIs can take advantage of the custom memory and thread management features.

> [!WARNING]
>
> You cannot mix XSAPI WinRT APIs with the C APIs.

## Useful resources

* [Samples using the XSAPI C APIs](https://github.com/Microsoft/xbox-live-samples)
* [libHttpClient GitHub repo](https://github.com/Microsoft/libHttpClient)
* To view the C APIs, see the XSAPI C header files. It is in the XBOX services SDK which is part of the GDK.

## See also

[XSAPI C reference](https://docs.xbox.com/reference/live/xsapi-c/atoc-xsapi-c.md)

[REST API reference](https://docs.xbox.com/reference/live/rest/atoc-xboxlivews-reference.md)

[HttpClient reference](https://docs.xbox.com/reference/live/httpclient/atoc-httpclient.md)


## Related topics

- [Unity overview](https://docs.xbox.com/paths/unity/overview.md)
- [Porting overview](https://docs.xbox.com/paths/porting/overview.md)
- [Privacy and permissions overview](https://docs.xbox.com/services/xbox-services/fundamentals/identity/privacy/live-privacy-overview.md)
- [Unreal Engine overview](https://docs.xbox.com/paths/unreal/overview.md)
- [XBOX Series X|S overview](https://docs.xbox.com/paths/series-xs/overview.md)
