Bolt Labs | API Library | Developer Resource

Logo

Bolt Labs API Library

Developer Resource

GitHub GitHub contributors GitHub release (release name instead of tag name)

Description

This lightweight library was specifically designed for UI-focused resource development. Throughout the process, we realized the need to repeatedly create boilerplate code to effectively communicate with the backend of our resources. In our search for a streamlined solution, we conceptualized a drag-and-drop library that places strong emphasis on simplicity and a straightforward data structure.

Join the Innovation on GitHub!

Check out our project’s repository at Github Repository. Feel free to explore the source code or contribute by creating a pull request. Join us in shaping the future of our project!

Getting Started

  1. Download the Library: Go to the release section and obtain the latest version of the library. It’s a single file, making it easy to download.
  2. Install the Library: Copy the file to your desired location within your resource.
  3. Configure Your Resource: Simply add the following snippet to your fxmanifest.lua file.
shared_scripts {
   "{YOUR_PATH}/api-lib.lua"
}

Usage

Client Middleware

To ensure registration of callbacks received from FiveM’s NUI, we need to create a client file. You can choose any name you prefer, but for the sake of simplicity, let’s name it middleware.lua. In this file, all you have to do is add the following method.

API:RegisterEndpoints({
    -- Endpoints
})

To register your desired endpoints, simply add them to the table as strings. It should follow a format similar to this example:

Important! You have the freedom to choose any names for these paths. In this demonstration, I’ll be using standard Web API conventions.

API:RegisterEndpoints({
    "/api/test",
    "/api/test/2"
})

Server Request

Adding your endpoints to the server is a straightforward process. Open the file where you want to define your route and add the following snippet at the top of the file (although it can be placed anywhere, the top is usually the easiest):

router = API.Router

It’s worth mentioning that you’re not required to follow this approach, but it helps keep your code clean and maintainable, especially as your resource expands.

Defining a Route

Defining a route is a simple process. By adding the following code, you can define your desired route:

-- Any value you return from this function will be sent back to the NUI.
router:Register("/api/test", function(req)
    local data = {}
    return data
end)

As you can see, this code snippet accepts one parameter. This parameter grants you access to the request data, which currently includes only the body for exchanging data between the server and NUI. In the future, additional request options will be supported.

What do you think about the resource?

  • Yes, it would be helpful.
  • No, don’t see a need for it.

0 voters

Dev-Tools
Dev-Tool
Developer Tool
Developer Tools
Dev tool
Dev tools
Open sources
Free
Nui

4 Likes

Hey, could you please provide a more complete case use example?

I am not sure if I am getting this right, is it a lib just to simplify the NUI callback process or it’s a lib that exposes a web server and routes (as in you do the calls directly from the js to the API)?

Thanks

No problem, this by no means spins up a web server at all. Pretty much all it is a convenient wrapper around the RegisterNUICallback Native.

client → middleware.lua

API:RegisterEndpoints({
    "getCharacters"
})

server → main.lua

router:Register("getCharacters", function(req)
    local characters= {}
    return characters
end)

When your calling the endpoint from the NUI (a.k.a javascript) the process stays the same.

web → index.js

fetch(`https://${GetParentResourceName()}}/getCharacters`, {
    body: JSON.stringify(/* Data Here */),
    method: "POST",
    headers: {
      "Content-Type": "application/json; charset=UTF-8",
    },
  });

Hope this clears everything up for you. This isn’t anything crazy complex just a simple solution for something we found annoying. Because we found ourselves having to write a RegisterNUICallback every time we needed to collect something from the database, and so we made a simple wrapper.

This is amazing!

Your answer definitively clears it, and it will come handy in the near future for me.

Thank you very much for the clarification and for making this open source!

No problem, we will be releasing tons of open source projects here. Feel free to keep up to date with us or join our discord for more updates.