R SDK
The official nRouter R SDK with R-universe preview, SSE streaming callbacks, and automated metadata extraction.
Last updated
The official nRouter R SDK (nrouter) provides an idiomatic interface for data scientists and researchers using R. Because there is no official OpenAI SDK for R, this package communicates directly with https://api.nrouter.ai/v1 via httr, offering full support for chat completions, token streaming callbacks, and metadata parsing.
Installation
Install the public preview package from R-universe:
install.packages(
"nrouter",
repos = c(
nroutergateway = "https://nroutergateway.r-universe.dev",
CRAN = "https://cloud.r-project.org"
)
)For development or bleeding-edge updates, install directly from the GitHub repository:
remotes::install_github("nRouterGateway/nrouter-sdk", subdir = "sdks/r")Setup & Authentication
Set your virtual API key in your environment or in your project's .Renviron file:
export NROUTER_API_KEY="sk-nrouter-your-api-key-here"In R code:
Sys.setenv(NROUTER_API_KEY = "sk-nrouter-your-api-key-here")Initialize the client targeting https://api.nrouter.ai/v1:
library(nrouter)
# Reads NROUTER_API_KEY from environment
client <- nrouter_client()You can also pass explicit parameters:
client <- nrouter_client(
api_key = "sk-nrouter-your-api-key-here",
base_url = "https://api.nrouter.ai/v1"
)Quickstart
Send a chat completion request using standard lists:
library(nrouter)
client <- nrouter_client()
result <- nrouter_chat_completions(client, list(
model = "gpt-5.4-mini",
messages = list(list(role = "user", content = "Hello!"))
))
# Print completion text
cat(result$body$choices[[1]]$message$content, "\n")
# Check cost
if (nrouter_is_priced(result$meta)) {
cat(sprintf("Cost: $%f\n", result$meta$cost))
}Streaming
The SDK supports real-time Server-Sent Events (SSE) streaming through callback functions:
nrouter_messages_stream(client, list(
model = "claude-haiku-4-5-20251001",
max_tokens = 64,
messages = list(list(role = "user", content = "Hello!"))
), function(chunk) {
cat(chunk$delta)
})Return FALSE from the callback function to terminate the stream early.
Error Handling
Errors are raised as classed R conditions, allowing precise condition handling with tryCatch:
tryCatch(
nrouter_chat_completions(client, body),
nrouter_guardrail_blocked_error = function(e) {
message("Request blocked by guardrails: ", conditionMessage(e))
},
nrouter_credit_error = function(e) {
message("Account balance exhausted: ", conditionMessage(e))
},
nrouter_rate_limit_error = function(e) {
if (nrouter_is_retryable(e)) {
message("Rate limit exceeded; retryable after backoff")
}
},
nrouter_error = function(e) {
message("nRouter general error: ", conditionMessage(e))
}
)| Class | HTTP Status | Description |
|---|---|---|
nrouter_request_error | 400 | Invalid request format |
nrouter_guardrail_blocked_error | 400 | Blocked by server-side guardrail check |
nrouter_authentication_error | 401 | Invalid or missing API key |
nrouter_credit_error | 402 | Out of credits or budget exceeded |
nrouter_not_found_error | 404 | Model not found |
nrouter_rate_limit_error | 429 | Rate limit or TPM quota exceeded |
nrouter_service_error | 503 | Gateway or upstream service unavailable |
nrouter_transport_error | — | Connection error |
Response Metadata & Cost Tracking
result$meta holds parsed x-nr-* gateway headers:
meta <- result$meta
cat("Request ID:", meta$request_id, "\n")
cat("Model:", meta$model, "\n")
if (nrouter_is_priced(meta)) {
cat(sprintf("Cost: $%f\n", meta$cost))
} else {
cat("Cost: unpriced\n")
}Source & Repository
The source code and runnable demonstrations are available on GitHub: