Skip to content

HTTP Endpoints

Every Membrane program comes with its own URL. By exporting an function you can quickly handle HTTP requests.

export const
const endpoint: (req: any) => string
endpoint
= (
req: any
req
) => {
switch (`${
req: any
req
.
any
method
} ${
req: any
req
.
any
path
}`) {
case "GET /":
return "hello world"
default:
return
var JSON: JSON

An intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.

JSON
.
JSON.stringify(value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string (+1 overload)

Converts a JavaScript value to a JavaScript Object Notation (JSON) string.

@paramvalue A JavaScript value, usually an object or array, to be converted.

@paramreplacer A function that transforms the results.

@paramspace Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.

stringify
({
status: number
status
: 404 })
}
}

When your program endpoint receives a request it will be logged, so you can inspect it in the Logs panel.

Accessing an endpoint URL

To access a program’s endpoint URL in your code, call:

await
const nodes: {
readonly clock: Clock;
readonly process: Process;
}

Contains the graph references (grefs) that this program has been given access to.

nodes
.
process: Process
process
.
Process.endpointUrl: Scalar<string>

The URL of the program's HTTP endpoint. await nodes.process.endpointUrl

endpointUrl

To open an endpoint, click “Open Preview” above the endpoint function signature. You can also copy a program’s endpoint by right clicking on it in the Navigator panel.

http program

In Membrane all communication is done via the graph. So how can fetch make network requests? Via a special program called http.

There is a special program running on your workspace called http. Whenever you use fetch, the request will go through the http program which ultimately makes the actual request.