REST API that you can use for free.
Get Started!Keep in mind that this API can be updated, without notification, but existing methods should work unless an unexpected exception occurs.
Check out Teothe, A 5E Campaign Setting website and their implementation for a very nice example of this API in use.
There is no easier way to kick the tires than through cURL. Let's start by testing our setup. Open up a command prompt and enter the following command:
$ curl http://xeculus.pythonanywhere.com/generate
> A story about a sentient old-fashioned shrine that desperately wants to rule the world.
or,
GET /generate HTTP/1.1
Host: xeculus.pythonanywhere.com
Using cURL, sending a request to this REST API is extremely easy.
The output you'll get is as shown, RAW. Since the API only returns a single text, it is easy to implement and use without parsing it or, whatever.
HTTP/1.1 200 OK
Date: Wed, 29 Sep 2021 19:40:30 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Clacks-Overhead: GNU Terry Pratchett
Server: PythonAnywhere
Content-Encoding: gzip
A story about a sentient old-fashioned shrine that desperately wants to rule the world.
You can use this output with JavaScript, also really easily. An example is provided below.
An example back-end code using Python would be as follows:
import requests
url = "http://xeculus.pythonanywhere.com/generate"
resp = requests.get(url)
print(resp.status_code)
print(resp.text)
You'll see that, after executing this code, you'll be displayed two lines.
> 200
> A story about a sentient old-fashioned shrine that desperately wants to rule the world.
Lucky for us, resp.text
is just a single line string and we can do whatever we want with it!
Let's now implement it to an HTML page, using JavaScript.
function getText(theUrl) {
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", theUrl, false); //Keep this false for discouraged synchronous request. Or don't.
xmlHttp.send(null);
return xmlHttp.responseText;
}
function updateText() {
document.getElementById('output.text').innerText = getText('/generate');
}
This is what the homepage uses to replace the text. You can of course write your own method, as long as you direct it to /generate
it should work.
Let's now use the NPC Generation section!
Lucky for us, npc generation works the same way as our /generate
endpoint works. Just call /npc_generate
and you are done. On the NPC related page, this is used a bit differently, by casting the string into an array and splicing it for formatting.
var textVal = getText("/npc_generate").split(". ")
document.getElementById("npc_output_text").innerText = textVal[0];
document.getElementById("npc_output_flavor").innerText = textVal.slice(1, -1).join(",\n");
For the quotes, just calling the /get_quote
endpoint will work.
Let's now utilize the name generator!
This endpoint uses query parameters ?category=VALUE
like so, after the /nameset
endpoint.
The VALUE can be
dwarf
elf
european
japanese
slavic
Using this is also really easy. Here is an example and output that is also on the NPC page! Using the getText
function from above...
console.log(getText("/nameset?category=japanese"));
> Makise Kurisu
Below examples will work, but need formatting since they return HTML elements and are discouraged, since if I get some time and if people need them, I'll update them to return a single text, with parameters, just like the /generate
method.
print(requests.get("http://xeculus.pythonanywhere.com/itemset").text)
print(requests.get("http://xeculus.pythonanywhere.com/encounterset").text)
Again, using these are not recommended since they return HTML elements and need some parsing or ReGeX.