Full documentation on the OutLayer dashboard.
WASM component that makes HTTP requests to OpenAI-compatible APIs using WASI Preview 2 HTTP support.
Sends HTTP POST request to OpenAI-compatible API and returns the response. Demonstrates WASI P2 component with HTTP capabilities running on NEAR OutLayer platform.
Key Features:
- Supports custom system prompts via environment variables (secrets)
- Works with any OpenAI-compatible API endpoint, named by the secrets' owner
- Configurable model, max tokens, and conversation history
- Secure API key management through encrypted secrets
{
"prompt": "What is NEAR Protocol?",
"history": [
{"role": "user", "content": "Previous question"},
{"role": "assistant", "content": "Previous answer"}
],
"model_name": "gpt-3.5-turbo",
"max_tokens": 150
}
Required fields:
prompt- User's question/prompt (string)
Optional fields:
history- Conversation history (array of messages, default: empty)model_name- Model name (default: "gpt-3.5-turbo")max_tokens- Maximum tokens in response (default: 150)
The endpoint is not an input field: an input carrying openai_endpoint is refused. The request
carries OPENAI_API_KEY as a bearer token, so its destination is set by the owner of the secrets
(OPENAI_ENDPOINT, below). An endpoint taken from the input would send the key to whoever runs
the code.
Plain text response from the AI model (not JSON-wrapped):
NEAR Protocol is a layer-1 blockchain designed for building decentralized applications...
# Add WASI P2 target
rustup target add wasm32-wasip2
# Build WASM component
cargo build --target wasm32-wasip2 --release
# Output: target/wasm32-wasip2/release/ai-example.wasm
# Build test runner (native binary)
cargo build --bin test_run --features test-runner
# Run test
./target/debug/test_run
# This will:
# - Load the WASM component
# - Execute it with wasmtime
# - Show output and fuel consumed
This WASM component reads sensitive data from environment variables, which should be stored as encrypted secrets:
OPENAI_API_KEY- Your OpenAI API key (or compatible API key)
OPENAI_ENDPOINT- Chat-completions URL,https://only (default:https://api.openai.com/v1/chat/completions)SYSTEM_PROMPT- Custom system prompt to control AI behavior
Example system prompts:
"You are a helpful blockchain expert. Answer questions about NEAR Protocol concisely."
"You are a code assistant. Provide code examples in Rust when relevant."
"You are a friendly teacher. Explain complex topics in simple terms."
outlayer upload target/wasm32-wasip2/release/ai-example.wasm
# prints SHA256 and: FastFS URL: https://test.fastfs.io/your-account.testnet/outlayer.testnet/<sha256>.wasm
outlayer deploy ai-example <FastFS URL>
Store your API key and system prompt as encrypted secrets under the project, using the dashboard or CLI:
Option A: Using Dashboard (Recommended)
- Open http://localhost:3000/secrets
- Connect your NEAR wallet
- Fill in the form:
- Project:
your-account.testnet/ai-example - Profile:
production - Secrets (JSON):
{ "OPENAI_API_KEY": "sk-...", "SYSTEM_PROMPT": "You are a helpful blockchain expert." } - Access Condition: Select who can use these secrets (e.g., AllowAll, Whitelist, NEAR balance check)
- Project:
- Click "Encrypt & Store Secrets"
Option B: Using the CLI
outlayer secrets set '{"OPENAI_API_KEY":"sk-proj-...","SYSTEM_PROMPT":"You are a helpful blockchain expert."}' \
--project your-account.testnet/ai-example --profile production
Option C: Store via NEAR CLI (secrets already ECIES-encrypted to the keystore key)
near call outlayer.testnet store_secrets '{
"accessor": { "Project": { "project_id": "your-account.testnet/ai-example" } },
"profile": "production",
"encrypted_secrets_base64": "<ECIES-encrypted JSON>",
"access": "AllowAll",
"vault_id": null
}' \
--accountId your-account.testnet \
--deposit 0.1
Call request_execution on the OutLayer contract:
Simple execution (no secrets):
near call outlayer.testnet request_execution '{
"source": {
"Project": { "project_id": "your-account.testnet/ai-example", "version_key": null }
},
"resource_limits": {
"max_instructions": 500000000,
"max_memory_mb": 128,
"max_execution_seconds": 60
},
"input_data": "{\"prompt\":\"What is NEAR Protocol?\"}"
}' --accountId your-account.testnet --deposit 0.1
With secrets (API key + system prompt):
near call outlayer.testnet request_execution '{
"source": {
"Project": { "project_id": "your-account.testnet/ai-example", "version_key": null }
},
"secrets_ref": {
"profile": "production",
"account_id": "your-account.testnet"
},
"resource_limits": {
"max_instructions": 500000000,
"max_memory_mb": 128,
"max_execution_seconds": 60
},
"input_data": "{\"prompt\":\"Explain smart contracts\",\"max_tokens\":300}"
}' --accountId your-account.testnet --deposit 0.1
With conversation history:
near call outlayer.testnet request_execution '{
"source": {
"Project": { "project_id": "your-account.testnet/ai-example", "version_key": null }
},
"secrets_ref": {
"profile": "production",
"account_id": "your-account.testnet"
},
"resource_limits": {
"max_instructions": 500000000,
"max_memory_mb": 128,
"max_execution_seconds": 60
},
"input_data": "{\"prompt\":\"What about gas fees?\",\"history\":[{\"role\":\"user\",\"content\":\"Tell me about NEAR\"},{\"role\":\"assistant\",\"content\":\"NEAR is a blockchain...\"}]}"
}' --accountId your-account.testnet --deposit 0.1
The worker will automatically:
- Fetch secrets from the contract (matching project + profile + owner)
- Validate access conditions via keystore
- Decrypt secrets via keystore (gets
OPENAI_API_KEYandSYSTEM_PROMPT) - Inject into WASI environment as environment variables
- Fetch the WASM of the active version and check it against its sha256
- Execute WASM with wasmtime (WASI P2 + HTTP support)
- Return AI response as plain text
- Submit result back to NEAR contract
Your WASM code automatically reads:
std::env::var("OPENAI_API_KEY")→ Gets decrypted API keystd::env::var("SYSTEM_PROMPT")→ Gets custom system prompt (if provided)
- ✅ WASI Preview 2 component
- ✅ HTTP/HTTPS requests support
- ✅ Fuel metering (instruction counting)
- ✅ JSON input/output via stdin/stdout
- ✅ Works with OpenAI-compatible APIs
- ✅ Custom system prompts via
SYSTEM_PROMPTenvironment variable - ✅ Encrypted secrets management via NEAR OutLayer
- ✅ API endpoint set by the secrets' owner; model and parameters set per call
- ✅ Conversation history support
- API keys are stored encrypted on NEAR blockchain
- Access control - owner defines who can use secrets (whitelist, balance checks, etc.)
- Secrets decryption happens in keystore worker (isolated from executor)
- Environment injection - secrets passed as WASI env vars (secure, ephemeral)
- No plaintext storage - API keys never stored in plaintext
Customer Support Bot:
{
"SYSTEM_PROMPT": "You are a friendly customer support agent for a DeFi protocol. Be helpful and concise.",
"OPENAI_API_KEY": "sk-..."
}
Code Assistant:
{
"SYSTEM_PROMPT": "You are an expert Rust developer. Provide code examples and best practices.",
"OPENAI_API_KEY": "sk-..."
}
Research Assistant:
{
"SYSTEM_PROMPT": "You are a blockchain research assistant. Provide accurate technical information with sources when possible.",
"OPENAI_API_KEY": "sk-..."
}
Another OpenAI-compatible API is named in the secrets, next to its key:
{
"OPENAI_API_KEY": "fw_...",
"OPENAI_ENDPOINT": "https://api.fireworks.ai/inference/v1/chat/completions"
}
The call then names a model that endpoint serves:
{
"prompt": "Explain NEAR Protocol",
"model_name": "accounts/fireworks/models/gpt-oss-120b",
"max_tokens": 500
}
Error: OPENAI_API_KEY not found
- Make sure you stored secrets on contract with correct project/profile
- Verify
secrets_refin yourrequest_executioncall matches stored secrets - Check access conditions allow your account to decrypt
Error: OpenAI API returned status 401
- Your API key is invalid or expired
- Update secrets with new API key using dashboard or CLI
Error: OpenAI API returned status 429
- Rate limit exceeded on your API key
- Wait a few moments and try again
- Consider upgrading your OpenAI plan
HTTP timeout
- Increase
max_execution_secondsin resource_limits - Default is 60 seconds, which should be sufficient for most queries
- Requires wasmtime 28+ for execution (worker uses wasmtime 27)
- HTTP requests require network access (allowed during execution)
- System prompt is optional - if not provided, OpenAI uses default behavior
- Maximum recommended
max_tokens: 4000 (to stay within instruction limits)
MIT OR Apache-2.0, at your option — see LICENSE-MIT and LICENSE-APACHE.