|
| 1 | +--- |
| 2 | +# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | +title: "TypeScript SDK" |
| 5 | +description: "Install and use the native TypeScript SDK for sandbox lifecycle, exec, SSH sessions, and port forwarding." |
| 6 | +keywords: "NVIDIA OpenShell, TypeScript SDK, Node.js, Connect, sandbox" |
| 7 | +--- |
| 8 | + |
| 9 | +The native TypeScript SDK connects Node.js applications to an OpenShell |
| 10 | +gateway. It requires Node.js 20.3 or later. |
| 11 | + |
| 12 | +Use the SDK and gateway from the same OpenShell release when possible. The raw |
| 13 | +types and RPC descriptors are generated from the protobuf definitions shipped |
| 14 | +with that release; curated methods remain compatible while their underlying |
| 15 | +RPC contracts remain compatible. |
| 16 | + |
| 17 | +## Install the package |
| 18 | + |
| 19 | +The pre-GA package is published through GitHub Packages. Configure the NVIDIA |
| 20 | +scope in your project-level `.npmrc`: |
| 21 | + |
| 22 | +```text |
| 23 | +@nvidia:registry=https://npm.pkg.github.com |
| 24 | +``` |
| 25 | + |
| 26 | +Authenticate npm with a GitHub token that has `read:packages`, then install the |
| 27 | +SDK: |
| 28 | + |
| 29 | +```shell |
| 30 | +npm install @nvidia/openshell-sdk |
| 31 | +``` |
| 32 | + |
| 33 | +## Connect to a gateway |
| 34 | + |
| 35 | +Create a client with the gateway URL and its authentication material: |
| 36 | + |
| 37 | +```typescript |
| 38 | +import { OpenShellClient } from "@nvidia/openshell-sdk"; |
| 39 | + |
| 40 | +const client = await OpenShellClient.connect({ |
| 41 | + gateway: "https://gateway.example.com", |
| 42 | + oidcToken: process.env.OPENSHELL_TOKEN, |
| 43 | +}); |
| 44 | + |
| 45 | +const health = await client.health(); |
| 46 | +console.log(health.status); |
| 47 | +``` |
| 48 | + |
| 49 | +`connect()` constructs a lazy client and does not issue a network request. Call |
| 50 | +`health()` when startup must verify gateway connectivity. |
| 51 | + |
| 52 | +Choose one authentication mode: |
| 53 | + |
| 54 | +- Set `clientCert` and `clientKey` together for mTLS. Use `caCert` to add a |
| 55 | + gateway CA certificate. |
| 56 | +- Set `oidcToken` for direct OIDC bearer authentication. |
| 57 | +- Set `edgeToken` for Cloudflare Access header authentication. |
| 58 | + |
| 59 | +Tokens are static for the lifetime of a client. Create a new client when a |
| 60 | +token refreshes. The SDK refuses to send a token over plaintext HTTP to a |
| 61 | +non-loopback host unless `allowInsecureAuth` is set explicitly. |
| 62 | + |
| 63 | +## Manage and execute in sandboxes |
| 64 | + |
| 65 | +Use `client.sandbox` for lifecycle and exec operations: |
| 66 | + |
| 67 | +```typescript |
| 68 | +const sandbox = await client.sandbox.create({ |
| 69 | + image: "ghcr.io/nvidia/openshell-community/sandboxes/python:latest", |
| 70 | +}); |
| 71 | + |
| 72 | +await client.sandbox.waitReady(sandbox.name, 120); |
| 73 | + |
| 74 | +const result = await client.sandbox.exec(sandbox.name, [ |
| 75 | + "/bin/sh", |
| 76 | + "-c", |
| 77 | + "python --version", |
| 78 | +]); |
| 79 | +process.stdout.write(result.stdout); |
| 80 | + |
| 81 | +await client.sandbox.delete(sandbox.name); |
| 82 | +await client.sandbox.waitDeleted(sandbox.name, 30); |
| 83 | +``` |
| 84 | + |
| 85 | +Pass an `AbortSignal` to wait, exec, interactive exec, and forward operations |
| 86 | +when the caller needs cancellation. `execStream()` yields stdout and stderr |
| 87 | +chunks followed by an in-band exit event. `execInteractive()` exposes raw |
| 88 | +stdin, resize, output, and completion primitives without taking ownership of |
| 89 | +terminal raw mode or signal forwarding. |
| 90 | + |
| 91 | +## Forward a sandbox service |
| 92 | + |
| 93 | +`forward()` creates a process-lifetime local TCP listener. Close the returned |
| 94 | +handle during application teardown: |
| 95 | + |
| 96 | +```typescript |
| 97 | +const forward = await client.sandbox.forward(sandbox.name, { |
| 98 | + targetPort: 8000, |
| 99 | + onConnectionError: (error) => console.error(error), |
| 100 | +}); |
| 101 | + |
| 102 | +console.log(`http://${forward.localHost}:${forward.localPort}`); |
| 103 | + |
| 104 | +await forward.close(); |
| 105 | +await forward.closed; |
| 106 | +``` |
| 107 | + |
| 108 | +Calling `close()` more than once is safe. It cancels active forwarding RPCs, |
| 109 | +closes accepted sockets, and waits for their cleanup. The root client has no |
| 110 | +explicit close method because the Connect transport does not retain a dedicated |
| 111 | +session; close operation-scoped streams and forward handles instead. |
| 112 | + |
| 113 | +## Use the raw gateway surface |
| 114 | + |
| 115 | +Curated helpers cover sandbox lifecycle, exec, forwarding, SSH sessions, |
| 116 | +sandbox provider attachments, settings, and policy updates. Use `client.raw` |
| 117 | +for gateway RPCs that do not yet have a curated helper, and import their |
| 118 | +generated types from the raw entry point: |
| 119 | + |
| 120 | +```typescript |
| 121 | +import type { GetGatewayConfigResponse } from "@nvidia/openshell-sdk/raw"; |
| 122 | + |
| 123 | +const config: GetGatewayConfigResponse = await client.raw.getGatewayConfig({}); |
| 124 | +``` |
| 125 | + |
| 126 | +The raw entry point exposes generated wire types. Prefer curated helpers when |
| 127 | +they cover the operation because those helpers preserve the SDK error taxonomy |
| 128 | +and normalize protobuf values for TypeScript callers. |
0 commit comments