Scripting API (ns)
Scripts are written in JavaScript and run inside an implicit async function main(ns) { ... } wrapper — so top-level await works, and ns is always in scope. There's no need to declare the function yourself; just write the body.
// Your code runs as the body of: async function main(ns) { <your code> }
ns.print('hello world');
await ns.sleep(1000);
Methods
ns.print(message: string): void
Writes a line to the script's output / the terminal, timestamped automatically. RAM cost: free.
ns.hack(target: string): Promise<number>
Attempts to hack the server named target. Resolves to the amount of money stolen (0 on failure or any error condition). See Hacking & Servers for the full success-chance formula, cooldowns, and rewards.
Key behavior to know before you loop this:
- If your hacking level is below the server's required skill, it prints an error, waits 1s, and returns 0.
- Only one hack can run against a given server at a time, across all your scripts (and manual hacking). If something else is already hacking that target, it prints an error, waits 1s, and returns 0 — it does not queue.
- If the server is on cooldown, it prints an error and waits out the *remaining* cooldown before returning 0.
- Each attempt takes about 1 second to resolve (simulated hacking time) before success/failure is determined.
- RAM cost: 1.5 GB (the most expensive single method).
ns.getServer(hostname: string): Server | null
Returns a snapshot (shallow copy) of a server's current state, or null if it doesn't exist. Fields: hostname, ip, securityLevel, money, maxMoney, hackDifficulty, requiredHackingSkill, connections. RAM cost: 0.5 GB.
ns.getServerMoney(hostname: string): number | null
Cheaper than ns.getServer() if all you need is current money — returns just that number, or null if the server doesn't exist. RAM cost: 0.2 GB.
ns.getPlayer(): Player
Returns a snapshot of your player stats: hacking, money, hackingExp, hackers, hackerCosts, machines, machineCosts, maxRam, ramUpgradeCost. RAM cost: 0.5 GB.
ns.sleep(ms: number): Promise<void>
Delays for ms milliseconds. Respects script stop/abort — if the script is stopped mid-sleep, the sleep rejects instead of resolving, which unwinds your script (you'll see "Script stopped by user" in the output). RAM cost: free.
ns.scan(hostname?: string): string[]
Lists reachable hostnames. Currently: calling it on 'home' (or with no argument) returns every server hostname except home itself; calling it on anything else returns ['home']. This is a simplification — it does not yet walk the actual connections graph. RAM cost: 0.2 GB.
ns.getTime(): number
Returns the current game tick counter (increments once per second of game time — see Progression). RAM cost: free.
Script RAM budget
Every script's RAM cost is computed statically by scanning its source text for calls to each ns. method — it's not based on runtime behavior, just which methods appear in the code (via a regex match on ns.<method>(, so calling a method from inside a loop or conditional costs the same as calling it once).
Cost formula: total = baseCost + sum of (methodCost for each ns.<method>(...) pattern found in the code, counted once per distinct method used)
| Component | RAM cost (GB) |
|---|---|
| Base cost (every script) | 1.6 |
ns.print, ns.getTime, ns.sleep | 0 (free) |
ns.scan | 0.2 |
ns.getServerMoney | 0.2 |
ns.getPlayer | 0.5 |
ns.getServer | 0.5 |
ns.hack | 1.5 |
So a script that only calls ns.hack and ns.sleep/ns.print costs 1.6 + 1.5 = 3.1 GB. A script that also calls ns.getServer and ns.scan costs 1.6 + 1.5 + 0.5 + 0.2 = 3.8 GB.
You can run multiple scripts simultaneously as long as the sum of their RAM costs doesn't exceed your maxRam (starts at 4 GB — see Resources for upgrading it). Trying to start a script that would exceed your budget fails outright and leaves whatever was already running untouched — it does not stop other scripts to make room.
Manual hacking
The Network Map lets you trigger a hack on a selected server without writing a script. It uses the exact same mechanics as ns.hack() (skill check, per-server lock, cooldown, success chance, rewards) — the only difference is it's outside the RAM/script economy, so it doesn't cost RAM or require a running script.