Fortnite! Unless you’re been on Mars, or living your life in a productive manner, you’ll have no doubt have come across this first-person shooter phenomenon. Pitted against 99 other players in a Battle Royale, all you have to do is be the last one standing. As a constantly evolving multiplayer game, there is plenty of data available on player stats, challenges, and items for sale.
Luckily, a simple Fortnite API provides all this information for us. We’re going to use a Sense HAT and Node-RED to create a news ticker for the most dedicated Fortnite players.
This tutorial was written by PJ Evans and first appeared in The MagPi magazine issue 78. Subscribe to The MagPi in print and get a free Raspberry Pi.
Fortnite API Sense HAT ticker: What you'll Need
- Raspberry Pi
- Sense HAT
- Epic Games user account
- Internet connection
- Plush Loot Llama (optional!)
Step 1. Prepare your Raspberry Pi
This project will work with any Raspberry Pi model that supports the Sense HAT. We need to use a web browser to create our code, so decide whether you’re going to do everything on the Pi itself or another machine. If the latter, you can install Raspbian Stretch Lite as your operating system and skip the graphical user interface. Make sure everything is up to date by opening Terminal and issuing this command:
sudo apt update && sudo apt -y upgrade
Ensure the Pi is connected to the internet and SSH is up and running if you’re going to be configuring from another machine.
Step 2. Introducing Node-RED
Our Fortnite device is going to use Node‑RED to generate and run our code. This amazing development environment is like Scratch for Internet-of-Things devices and is deceptively powerful. If you’ve installed ‘full’ Raspbian with all the recommended software, Node-RED will already be installed and can be found under Programming in the desktop applications menu. Otherwise, follow the command-line instructions at Node-RED before continuing. Once installed, set Node-RED to always run on boot:
sudo systemctl enable nodered.service
To test the installation, reboot the Raspberry Pi, then open a web browser and point it to
http://[IP-Address-Of-Your-Pi]:1880
(or http://localhost:1880 on Raspbian desktop).
Step 3. Install Sense HAT node
Programs in Node-RED are known as flows and are made up of ‘nodes’ that perform specific actions then pass the results of that action (the ‘message’) to the next node. Node-RED comes with a library of nodes to handle input, output, and steps required in between. To interact with the Sense HAT, we need to install an additional node and some other libraries. Enter the following line-by-line at the Terminal:
cd ~/.node-red
sudo apt install -y python-pip sense-hat
sudo pip install pillow
npm install node-red-node-pi-sense-hat
node-red-restart
Step 4. Your first flow
Flows need to start with a trigger: an event that starts the flow. From the left-hand list of nodes, drag ‘inject’ across to the centre grid (it will change to ‘timestamp’) then scroll down to the ‘Raspberry Pi’ section and there should be our Sense HAT. Drag over the ‘output’ node (small square on the left) and drag a link between the two. Click ‘Deploy’ followed by the button on the left of the ‘timestamp’ node. If all is well, you’ll see a long number scrolling across the Sense HAT display. Well done, you’ve written your first Node-RED flow.
Step 5. Connect to the Fortnite API
Although Epic Games does not have an ‘official’ API, others have provided access for us. One of the most popular is Fortnite API, which is also one of the easiest to implement. We’ll make two HTTP calls to the API to get a user’s details, then look up their current score and output it to the Sense HAT. Open up the Node-RED editor as before in your web browser, remove any nodes remaining from the test exercise, then go through the steps in the ‘Code Flow’ box to create your new flow. Alternatively, download the flow from GitHub and use Menu > Import > Clipboard to load the flow in.
Step 6. Add more functions
Now you can view a user’s score just by pressing the joystick button. However, Node-RED flows don’t have to be linear, so you can process several actions to produce different reactions and branch code based on the incoming data. Download the ‘advanced’ flow from GitHub and import using Menu > Import > Clipboard. Have a look at how each step is configured and see how you could change things to add new features or enhance existing displays. Don’t forget, you can use Node‑RED to talk to thousands of different APIs!
Create your Fortnite Flow
To create your Fortnite flow, go through each step below, drag the required node onto the grid, link it to the previous node moving from left to right. Set the properties by double-clicking on the node and completing the form fields as given. Leave all other fields at their default settings.
- Language: Node-Red
- Download: score.json
Sense HAT Input Output: Joystick events only Switch Name: Joystick pressed? Property drop-down: Expression Property field: payload[key='ENTER'] and payload[state=0] HTTP Request URL: https://fortnite-public-api.theapinetwork.com/prod09/users/id?username=USERNAME (N.B. Replace USERNAME with your Epic Games account name) Return: a parsed JSON object Name: Call User Info Template Name: Extract User ID Set property: url (leave drop-down as ‘msg’) Template: https://fortnite-public-api.theapinetwork.com/prod09/users/public/br_stats?user_id={{payload.uid}}&platform=pc HTTP Request Return: a parsed JSON object Name: Call User Stats Template Name: Create string from stats Template: Solo Score: {{payload.stats.score_solo}} Sense HAT Output Name: Display score