Script Requests API

experimental

WARNING

The Script API is currently in active development, and breaking changes are frequent. This page assumes the format of Minecraft beta 1.19.70

WARNING

This module can only be used on Bedrock Dedicated Server.

In Scripting API, you can send and receive HTTP-based requests to interact with the internet. For more detailed information please visit Microsoft docs.

Setup

Dependencies

Like other modules, you will need to add the dependency into your manifest.json

json
{
	"dependencies": [
		{
			"module_name": "@minecraft/server-net",
			"version": "1.0.0-beta"
		}
	]
}
1
2
3
4
5
6
7
8

Enable module in Bedrock Dedicated Server

  1. Download the Bedrock Server from the Minecraft website

  2. Extract the zip file on a folder.

This is the tree for default Bedrock Dedicated Server:

šŸ“BedrockServer
šŸ“behavior_packs
šŸ“config
šŸ“default
šŸ“permissions.json
šŸ“definitions
šŸ“development_behavior_packs
šŸ“development_resource_packs
šŸ“development_skin_packs
šŸ“resource_packs
šŸ“structures
šŸ“worlds
šŸ“BedrockLevel
šŸ“behavior_packs
šŸ“db
šŸ“resource_packs
šŸ“world_templates
  1. In the permissions.json file located in config/<pack_id>/permissions.json or config/default/permissions.json, enable @minecraft/server-net module by adding "@minecraft/server-net" in the allowed_modules key.
  • Modify files in default folder allows every add-ons with server-net module enabled in dependencies have access to @minecraft/server-net module.
  • It is recommended to assign appropriate permissions for each script behavior pack.
BedrockServer/config/default/permissions.jsonCopy
json
{
  "allowed_modules": [
    "@minecraft/server-gametest",
    "@minecraft/server",
    "@minecraft/server-ui",
    "@minecraft/server-admin",
    "@minecraft/server-editor",
    "@minecraft/server-net"
  ]
}
1
2
3
4
5
6
7
8
9
10

Http Request Methods

Minecraft API supports the following HTTP request methods:

Simple HTTP Request

http.get(url) - Performs a simple HTTP get request in behavior packs.

Since most requests are GET requests without bodies, @minecraft/server-net provides this convenience method. The only difference between this method and http.request() is that it sets the method to GET automatically.

Example:

js
import { http } from '@minecraft/server-net';

http.get('http://example.com/').then((response) => {
  // Body content of the HTTP response.
  // Type: string
  const body = response.body;
});
1
2
3
4
5
6
7

Advanced HTTP Request

http.request

Makes a request to a web server.

Config must be a new HttpRequest instance in order to structuring a request.

Examples

Here are the following ways to send a request to a web server, includes each available http request methods.

Create a HttpRequest object

js
import { http } from "@minecraft/server-net";

const request = new HttpRequest("http://localhost:8000/"); // You must put a url in parameter
1
2
3

Set HTTP method

More information on HTTP request methods: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods

GET

js
import { HttpRequestMethod } from '@minecraft/server-net';
request.method = HttpRequestMethod.GET;
1
2

HEAD

js
import { HttpRequestMethod } from '@minecraft/server-net';
request.method = HttpRequestMethod.HEAD;
1
2

POST

js
import { HttpRequestMethod } from '@minecraft/server-net';
request.method = HttpRequestMethod.POST;
1
2

PUT

js
import { HttpRequestMethod } from '@minecraft/server-net';
request.method = HttpRequestMethod.PUT;
1
2

DELETE

js
import { HttpRequestMethod } from '@minecraft/server-net';
request.method = HttpRequestMethod.DELETE;
1
2

Set HTTP headers

A HTTP header can be used in an HTTP request to provide information about the request context, so that the server can tailor the response.

js
import { HttpHeader } from '@minecraft/server-net';
request.headers = [
    new HttpHeader("Content-Type", "application/json"),
    new HttpHeader("auth", "my-auth-token"),
];
1
2
3
4
5

HttpHeader value parameter also accept SecretString object in '@minecraft/server-admin' module.

js
import { HttpHeader } from '@minecraft/server-net';
import { secrets } from '@minecraft/server-admin';

const secret = secrets.get('TOKEN');
request.headers = [
  new HttpHeader('Authorization', secret)
];
1
2
3
4
5
6
7

Set request body

Content of the body of the HTTP request, this information will be sent to a web server.

js
request.body = 'Message';
1

Set response timeout

Set the amount of time, in seconds, before the request times out and is abandoned.

This property is not frequently used in HTTP requests.

js
request.timeout = 10; // 10 seconds
1

Send request

Send the request to a web server, returns a promise HttpResponse.

js
http.request(request).then((response) => {
  // Body content of the HTTP response.
  // Type: string
  response.body;
});
1
2
3
4
5

Example:

js
import { http } from "@minecraft/server-net";

const request = new HttpRequest("http://example.com/");
request.method = HttpRequestMethod.POST;
request.body = 'body';
request.headers = [
  new HttpHeader("Content-Type", "application/json"),
  new HttpHeader("auth", "my-auth-token"),
];

http.request(request).then((response) => {
  // Body content of the HTTP response.
  // Type: string
  response.body;
});

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

Original Credit

Contributors