Skip to main content

JavaScript mim Runtime API

The JavaScript mim Runtime API provides the objects and methods available to your mim code at runtime. Every mim request handler receives three objects:

mimikModule.exports = (context, request, response) => {
// context - runtime services (storage, http, env, info)
// request - incoming HTTP request data
// response - methods to send HTTP response
};

Handler Signature

Your mim exports a single function that mimOE calls for each incoming request:

import Router from 'router';

const app = Router({ mergeParams: true });

mimikModule.exports = (context, request, response) => {
global.context = context; // Make context available globally
app(request, response, (e) => {
response.end(JSON.stringify({ code: e ? 400 : 404, message: e || 'Not Found' }));
});
};

app.get('/hello', (request, response) => {
response.end('Hello World!');
});

app.get('/data', (request, response) => {
const value = context.storage.getItem('myKey');
response.end(JSON.stringify({ value }));
});

The Three Objects

context

The context object provides runtime services for your mim:

PropertyTypeDescription
context.httpobjectHTTP client for outbound requests
context.storageobjectPersistent key-value storage
context.envobjectEnvironment variables passed at deployment
context.infoobjectMetadata about the mim and node
context.dispatchWebSocketEventfunctionDispatch WebSocket events (when enabled)

request

The request object contains the incoming HTTP request:

PropertyTypeDescription
request.urlstringFull URL including query string
request.methodstringHTTP method (GET, POST, etc.)
request.headersobjectHTTP headers (lowercase keys)
request.bodystringRequest body (for POST/PUT)
request.authorizationstringAuthorization header value

response

The response object is used to send the HTTP response:

Property/MethodDescription
response.statusCodeSet HTTP status code (default: 200)
response.setHeader(key, value)Set a response header
response.write(content)Write to response stream
response.end(body)Send response and close connection

Quick Examples

Simple GET endpoint

app.get('/status', (request, response) => {
response.end(JSON.stringify({ status: 'ok' }));
});

POST with JSON body

app.post('/users', (request, response) => {
const user = JSON.parse(request.body);
context.storage.setItem('user:' + user.id, JSON.stringify(user));
response.statusCode = 201;
response.end(JSON.stringify({ created: true }));
});

Making outbound HTTP requests

app.get('/external', (request, response) => {
context.http.request({
url: 'https://api.example.com/data',
success: (result) => {
response.end(result.data);
},
error: (err) => {
response.statusCode = 500;
response.end(err.message);
}
});
});

Using environment variables

app.get('/config', (request, response) => {
const apiKey = context.env.API_KEY;
const debug = context.env.DEBUG === 'true';
response.end(JSON.stringify({ debug }));
});

API Reference

PageDescription
context.httpHTTP client for outbound requests
context.storagePersistent key-value storage with tagging, embeddings, and pagination
context.infoNode metadata and environment variables
context.dispatchWebSocketEventWebSocket event dispatch for real-time notifications
requestIncoming HTTP request object
responseHTTP response methods

Runtime Environment

Critical Understanding

The mimOE JavaScript runtime is NOT Node.js. It is a custom ES5-based runtime. Node.js built-in modules are not available.

ES5 with Transpilation

The runtime executes ES5 JavaScript, but you can write modern JavaScript and transpile it using Webpack with Babel:

// You can write modern JavaScript
const add = (a, b) => a + b;
const message = `Hello ${name}`;
const { data } = response;

// Webpack + Babel transpiles to ES5 for deployment

See the JavaScript mims guide for toolchain setup.

What's Available vs. What's Not

AvailableNot Available
context.httpfetch, XMLHttpRequest
context.storagefs, localStorage
JSON, Math, Date, RegExpsetTimeout, setInterval
Array methods (map, filter, etc.)Buffer (Node.js)
console.log (debugging)process, __dirname

For the full list of constraints and workarounds, see Platform Constraints.