CS2API 1.1.0
dotnet add package CS2API --version 1.1.0
NuGet\Install-Package CS2API -Version 1.1.0
<PackageReference Include="CS2API" Version="1.1.0" />
<PackageVersion Include="CS2API" Version="1.1.0" />
<PackageReference Include="CS2API" />
paket add CS2API --version 1.1.0
#r "nuget: CS2API, 1.1.0"
#:package CS2API@1.1.0
#addin nuget:?package=CS2API&version=1.1.0
#tool nuget:?package=CS2API&version=1.1.0
CS2API
A C# wrapper and transpiler for Counter-Strike 2's Workshop JavaScript API. Write your point_script scripts in C# with full type safety and IntelliSense, then automatically transpile them to clean JS.
Why?
CS2 Workshop scripts are written in JavaScript using the point_script entity API. CS2API lets you write those scripts in C# instead, giving you:
- Type safety -- catch errors at compile time, not in-game
- IntelliSense -- full autocomplete for every API method, event, and type
- Clean output -- transpiles to dependency-free JS that runs directly in CS2's V8 environment
Installation
dotnet add package CS2API
The package includes both the API stubs (for writing scripts) and the compiler (for transpiling to JS). After building your project, the transpiler runs automatically and outputs .js files to a Scripts/ directory.
Quick Start
Create a new console or class library project and add the CS2API package. Then write a script:
using CS2API;
using CS2API.Data;
using CS2API.Enumerations;
namespace MyWorkshopMap;
public class Script
{
public static void Main()
{
Instance.Msg("Hello from C#!");
Instance.OnRoundStart(() =>
{
Instance.Msg("Round started!");
});
Instance.OnPlayerChat((e) =>
{
Instance.Msg($"Player said: {e.text}");
});
Instance.EntFireAtName("cover", "Enable");
}
}
This transpiles to:
import { Instance } from "cs_script/point_script";
Instance.Msg("Hello from C#!");
Instance.OnRoundStart(() => {
Instance.Msg("Round started!");
});
Instance.OnPlayerChat((e) => {
Instance.Msg(`Player said: ${e.text}`);
});
Instance.EntFireAtName({ name: "cover", input: "Enable" });
Configuration
By default, the transpiler runs after every build and outputs to Scripts/. You can customize this in your .csproj:
<PropertyGroup>
<CS2APIOutputDir>$(MSBuildProjectDirectory)\MyScripts</CS2APIOutputDir>
<CS2APICompilerEnabled>false</CS2APICompilerEnabled>
</PropertyGroup>
Type Mappings
| C# | JS |
|---|---|
Instance.Msg(text) |
Instance.Msg(text) |
new Vector(1, 2, 3) |
{ x: 1, y: 2, z: 3 } |
new Color(255, 0, 0) |
{ r: 255, g: 0, b: 0 } |
$"Hello {name}" |
`Hello ${name}` |
player?.GetPawn() |
player?.GetPawn() |
obj!.Method() |
obj.Method() |
(int)expr |
expr |
Math.Floor(x) |
Math.floor(x) |
Math.PI |
Math.PI |
Random.Shared.NextDouble() |
Math.random() |
== / != |
=== / !== |
new List<int>() |
[] |
list.Add(x) |
list.push(x) |
list.Contains(x) |
list.includes(x) |
list.Count |
list.length |
list.RemoveAt(i) |
list.splice(i, 1) |
list.Insert(i, x) |
list.splice(i, 0, x) |
default / default(T) |
undefined |
CSWeaponType.SniperRifle |
CSWeaponType.SNIPER_RIFLE |
(a, b) (tuple) |
{ a: a, b: b } |
C# equality operators are automatically converted to strict equality in JS. Casts, null-forgiving operators (!), numeric suffixes (1.5f), and generic type arguments are all stripped during transpilation.
Math and Random
Use System.Math directly -- all methods are automatically camelCased for JS (Math.Floor → Math.floor, Math.Cos → Math.cos). Constants like Math.PI are preserved as-is.
For random numbers, use Random.Shared.NextDouble() which transpiles to Math.random().
Supported Constructs
The transpiler supports the following C# constructs:
- Variables --
var, typed declarations,const - Control flow --
if/else if/else,switch/case/default, ternary (a ? b : c) - Loops --
for,foreach(->for...of),while,do...while,break,continue - Functions -- local functions, class methods, lambdas (both
() => {}andx => {}) - Collections --
List<T>withAdd,Contains,IndexOf,RemoveAt,Insert,Count; collection expressions ([1, 2, 3]); element access (list[i]) - Object creation --
new Vector(x, y, z)→ JS object literals; object initializers (new Config { Prop = val }) - Strings -- interpolation (
$"text {expr}"),System.Stringmethods (auto-camelCased) - Enums -- CS2API enums auto-convert to
UPPER_SNAKE_CASEand are added to the import - Config-object methods -- methods like
EntFireAtNameautomatically wrap arguments into a JS object literal - Tuples -- transpiled to JS objects using element names
- Namespaces and classes -- stripped (JS has no equivalent)
Not yet supported
async/await- LINQ (
Where,Select, etc.) try/catch/finally- Pattern matching (
switchexpressions, complexispatterns) string.Format(use string interpolation instead)
Project Structure
Your C# scripts should follow this pattern:
- Namespace -- used for organization, stripped in output
Scriptclass -- the wrapper class, unwrapped in outputMain()method -- the entry point, its body becomes top-level statements- Other methods -- transpiled to JS
functiondeclarations - Fields -- transpiled to top-level
let/constdeclarations
License
See LICENSE.md for details.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.