send get request to <url>since 1.4.0Fetch a URL; the value is the response body as text
set {_response} to send get request to "https://api.mojang.com/users/profiles/minecraft/" + urlencoded arg.1Try it← All of the Docs Data and the outside
send get request to <url>since 1.4.0Fetch a URL; the value is the response body as text
set {_response} to send get request to "https://api.mojang.com/users/profiles/minecraft/" + urlencoded arg.1Try itsend post request to <url> with body <text>since 1.4.0Send data. A body starting with { or [ is sent as JSON, anything else as plain text
send post request to "https://api.example.com/v1/analytics" with body "{\"event\":\"command_used\",\"player\":\"%player.name%\"}"Try itheaders <text>, <text>, ...since 1.4.0Optional, after the body: lines such as "Authorization: Bearer abc"
async:
send post request to "https://api.example.com/v1/events" with body "{\"type\":\"join\"}" headers "Authorization: Bearer abc123"Try it{_body}since 1.7.17What was sent, as text; json value "<path>" of {_body} reads from it if it is JSON
on web request "/announce":
broadcast "&e[Web] &f%{_body}%"Try it{_method}since 1.7.17GET, POST, ...
on web request "/ping":
log "%{_method}% from %{_address}%"Try it{_path}since 1.7.17The address asked for, e.g. /vote
on web request:
log "Something asked for %{_path}%"Try it{_query}since 1.7.17What came after ? in the address, without the token
on web request "/give":
log "Asked with: %{_query}%"Try it{_address}since 1.7.17Where the request came from
on web request "/vote":
log "A vote arrived from %{_address}%"Try itScripts can call web APIs. A request waits for the server on the other end, so one whose answer you use has to be inside an async: block; HyperScript refuses to run it on the server thread rather than freeze the game.
command /lookup <player>:
trigger:
send "&eFetching data from the API..."
async:
set {_response} to send get request to "https://api.mojang.com/users/profiles/minecraft/" + urlencoded arg.1
set {_uuid} to json value "id" of {_response}
sync:
if {_uuid} is set:
send "&aFound UUID for %arg.1%: &f%{_uuid}%"
else:
send "&cPlayer profile not found."
command /postanalytics:
trigger:
send post request to "https://api.example.com/v1/analytics" with body "{\"event\":\"command_used\",\"player\":\"%player.name%\"}"
| Syntax | Meaning |
|---|---|
send get request to <url> | Fetch a URL; the value is the response body as text |
send post request to <url> with body <text> | Send data. A body starting with { or [ is sent as JSON, anything else as plain text |
headers <text>, <text>, ... | Optional, after the body: lines such as "Authorization: Bearer abc" |
set {_r} to send get request ...), a request must be in async:. Used as a statement, where nothing reads the answer, it may appear anywhere and is sent in the background.json value "<path>" of <text> picks a value out of it, as the example does; see below.urlencoded before adding it to a URL, as above. Otherwise a player can type characters that change which address or parameters the request uses.localhost, 192.168.x.x, 10.x.x.x and similar) are refused, so a URL built from player input cannot be used to reach private services. Set web.allow-private-addresses: true in config.yml if your scripts need to call something on your own network.web.enabled: false turns web requests off.Reading JSON. json value takes a path and JSON text and gives the value found there.
set {_json} to "{\"name\":\"Steve\",\"stats\":{\"kills\":12},\"friends\":[\"Alex\",\"Bob\"]}"
log json value "name" of {_json}
log json value "stats.kills" of {_json}
log json value "friends.2" of {_json}
log size of (json value "friends" of {_json})
That logs Steve, 12, Bob and 2.
| Path step | Picks |
|---|---|
a name, such as stats | That field of an object. Capitals are ignored if there is no exact match |
a number, such as 2 | That element of a list, counting from 1 like the rest of HyperScript |
steps joined by dots, stats.kills | One inside the other |
json value.if {_value} is set: tells you whether it worked.Being called from outside. HyperScript can also listen: a voting site, a web shop or a Discord bot calls an address of the server, and a script runs.
on web request "/vote":
set {_name} to json value "username" of {_body}
broadcast "&a%{_name}% voted for the server!"
It is off until web.server.enabled is true in config.yml, and it needs web.server.token set to a secret of at least 12 characters. The address is then http://<your server>:8765/vote (the port is web.server.port), and every request has to carry the token: as a header Authorization: Bearer <token> or X-Token: <token>, or as ?token=<token> in the address.
| In the block | What it holds |
|---|---|
{_body} | What was sent, as text; json value "<path>" of {_body} reads from it if it is JSON |
{_method} | GET, POST, ... |
{_path} | The address asked for, e.g. /vote |
{_query} | What came after ? in the address, without the token |
{_address} | Where the request came from |
on web request: without an address hears every request.ok when a block ran and 404 when none listens at that address; a script cannot choose the answer. Without the right token the answer is 401 and no script runs.bind: "127.0.0.1" when it is.