context.info & context.env
The context.info object contains metadata about the current mim and the mimOE node it's running on. The context.env object contains environment variables passed at deployment time.
context.info
Read-only properties populated by the runtime.
Properties
| Property | Type | Description |
|---|---|---|
serviceType | string | The service type of the mim |
apiRoot | string | The API root path where the mim is mounted |
httpPort | string | The HTTP port that mimOE is listening on |
accountId | string | The account ID associated with the mimOE node |
nodeId | string | The unique identifier of the mimOE node |
serviceType
The service type identifier for this mim.
app.get('/info', (request, response) => {
response.end(JSON.stringify({
serviceType: context.info.serviceType
}));
});
apiRoot
The base path where this mim is mounted. Useful for constructing URLs to your own endpoints.
app.get('/info', (request, response) => {
var root = context.info.apiRoot;
// e.g., "/my-service/v1"
response.end(JSON.stringify({
apiRoot: root,
fullUrl: 'http://localhost:' + context.info.httpPort + root
}));
});
httpPort
The HTTP port that mimOE is listening on. Use this to call other mims or services on the same node.
app.get('/call-other', (request, response) => {
var port = context.info.httpPort;
context.http.request({
url: 'http://127.0.0.1:' + port + '/other-mim/v1/data',
success: function(result) {
response.end(result.data);
},
error: function(err) {
response.statusCode = 500;
response.end(err.message);
}
});
});
accountId
The account ID associated with this mimOE node. Useful for multi-tenant applications.
app.get('/info', (request, response) => {
response.end(JSON.stringify({
accountId: context.info.accountId
}));
});
nodeId
The unique identifier of this mimOE node. Useful for logging, debugging, and distributed systems.
app.get('/info', (request, response) => {
response.end(JSON.stringify({
nodeId: context.info.nodeId
}));
});
Full Example
app.get('/node-info', (request, response) => {
response.end(JSON.stringify({
serviceType: context.info.serviceType,
apiRoot: context.info.apiRoot,
httpPort: context.info.httpPort,
accountId: context.info.accountId,
nodeId: context.info.nodeId
}));
});
context.env
Environment variables passed to the mim at deployment time. Access variables using dot notation.
Usage
// Access environment variables
var apiKey = context.env.API_KEY;
var debugMode = context.env.DEBUG === 'true';
var maxRetries = parseInt(context.env.MAX_RETRIES || '3');
Setting Environment Variables
Environment variables are set when deploying the mim via the MCM API:
curl -X POST http://localhost:8083/mcm/v1/containers \
-H "Content-Type: application/json" \
-d '{
"name": "my-mim",
"image": "my-mim-v1",
"env": {
"API_KEY": "secret123",
"DEBUG": "true",
"MAX_RETRIES": "5",
"EXTERNAL_API_URL": "https://api.example.com"
}
}'
Example: Configuration Endpoint
app.get('/config', (request, response) => {
var apiKey = context.env.API_KEY;
var debug = context.env.DEBUG === 'true';
response.end(JSON.stringify({
debug: debug,
hasApiKey: !!apiKey
}));
});
Example: Using Environment Variables for External APIs
app.get('/external', (request, response) => {
var apiUrl = context.env.EXTERNAL_API_URL;
var apiKey = context.env.EXTERNAL_API_KEY;
if (!apiUrl || !apiKey) {
response.statusCode = 500;
response.end('Missing configuration');
return;
}
context.http.request({
url: apiUrl + '/data',
authorization: 'Bearer ' + apiKey,
success: function(result) {
response.end(result.data);
},
error: function(err) {
response.statusCode = 500;
response.end(err.message);
}
});
});
Example: Feature Flags
app.get('/feature', (request, response) => {
var newFeatureEnabled = context.env.ENABLE_NEW_FEATURE === 'true';
if (newFeatureEnabled) {
// New feature logic
response.end(JSON.stringify({ version: 'v2', feature: 'enabled' }));
} else {
// Legacy logic
response.end(JSON.stringify({ version: 'v1', feature: 'disabled' }));
}
});
Best Practices
Use Environment Variables for Secrets
Never hardcode credentials in your mim code:
// Good
var apiKey = context.env.API_KEY;
// Bad
var apiKey = 'hardcoded-secret-key';
Provide Defaults for Optional Variables
var timeout = parseInt(context.env.TIMEOUT || '30000');
var maxItems = parseInt(context.env.MAX_ITEMS || '100');
var logLevel = context.env.LOG_LEVEL || 'info';
Validate Required Variables
app.get('/start', (request, response) => {
var requiredVars = ['API_KEY', 'DATABASE_URL'];
var missing = [];
requiredVars.forEach(function(varName) {
if (!context.env[varName]) {
missing.push(varName);
}
});
if (missing.length > 0) {
response.statusCode = 500;
response.end(JSON.stringify({
error: 'Missing required environment variables',
missing: missing
}));
return;
}
// Continue with initialization
response.end('OK');
});
Related
- context.http: HTTP client API
- context.storage: Persistent storage API
- request: Incoming request object
- response: Response methods
- MCM API: Deploy mims with environment variables