TypeScript/Node.js client for Piston code execution engine API with WebSocket support
npm install piston-apiA comprehensive TypeScript client for the Piston API with WebSocket support for interactive code execution.
- 🔥 Simple API: Execute code with one line
- 🌐 Interactive Sessions: Real-time WebSocket communication
- 📝 Input Support: Send input to running programs
- 🛡️ Type Safety: Full TypeScript support with Zod validation
- 📊 Event-Driven: Listen to session lifecycle events
- 🚀 High-Level: Automatic session management
``bash`
npm install
npm run build
`typescript
import { PistonClient } from "./src/managers/client";
const piston = new PistonClient({ baseUrl: "http://localhost:2000" });
// Execute Python code
const result = await piston.run({
language: "python",
version: "*",
code: 'print("Hello, World!")',
});
console.log(result.run.stdout); // "Hello, World!"
// Execute with input
const result2 = await piston.run({
language: "python",
version: "*",
code: "print(input())",
stdin: "User Input",
});
console.log(result2.run.stdout); // "User Input"
`
`typescript
// Create interactive session
const sessionId = await piston.runInteractive(
while True:
expr = input("> ")
if expr == 'quit':
break
print(f"= {eval(expr)}"),
"python"
);
// Listen for output
piston.on("sessionOutput", (id, output) => {
console.log("Output:", output);
});
// Send input
piston.sendInput(sessionId, "2 + 3\\n");
piston.sendInput(sessionId, "quit\\n");
// Cleanup
piston.destroySession(sessionId);
`
`typescript
// C++ calculator
const result = await piston.run({
language: "cpp",
version: "*",
code:
#include
using namespace std;
int main() {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
cout << "Sum: " << (a + b) << endl;
return 0;
},
stdin: "10\\n5\\n",
});
console.log(result.run.stdout); // "Enter two numbers: Sum: 15"
`
#### Methods
- run(request) - Execute code oncerunInteractive(code, language)
- - Start interactive sessionsendInput(sessionId, input)
- - Send input to sessiondestroySession(sessionId)
- - End sessiongetRuntimes(input?, force?)
- - Get available languagesinstallPackage(request)
- - Install specific package versioninstallRuntime(language)
- - Install latest version of a runtimevalidateInstance()
- - Check if Piston is available
#### Events
- sessionCreated - Session startedsessionReady
- - Session ready for inputsessionOutput
- - Session produced outputsessionCompleted
- - Session endedsessionError
- - Session error occurredsessionDestroyed
- - Session cleaned up
The client supports all languages available in Piston:
- Python (python, py)javascript
- JavaScript (, js, node)c++
- C++ ()c
- C ()java
- Java ()go
- Go ()rust
- Rust ()
- And many more!
Check available runtimes:
`typescript${r.language} ${r.version}
const runtimes = await piston.getRuntimes();
runtimes.forEach((r) => console.log());
// Install latest version of a runtime
await piston.installRuntime("python");
// Install specific package version
await piston.installPackage({ language: "python", version: "3.11.0" });
``
This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.