Anyone know how to do this? I tried with Groks help but was getting missing lua errors.
No idea. There’s https://defold.com/llms-full.txt which should be easily ingestible by LLMs. What are the specific errors you are getting?
local json = require("builtins.scripts.json")
function init(self)
local url = "https://api.x.ai/v1/chat/completions"
local method = "POST"
local api_key = "xai-"
local headers = {
["Authorization"] = "Bearer " .. api_key,
["Content-Type"] = "application/json"
}
local request_body = {
model = "grok-4-latest",
messages = {
{ role = "system", content = "You are a test assistant." },
{ role = "user", content = "Testing. Just say hi and hello world and nothing else." }
},
stream = false,
temperature = 0
}
local post_data = json.encode(request_body)
print("Encoded JSON: " .. post_data)
http.request(url, method, function(self, id, response)
print("Status: " .. response.status)
if response.status == 200 then
local data = json.decode(response.response)
local completion = data.choices[1].message.content
print("Grok Response: " .. completion)
else
local error_msg = response.response and json.decode(response.response) or { message = "No details" }
print("API Test Failed: " .. response.status .. " - " .. (error_msg.message or "Unknown error"))
end
end, headers, post_data)
end
Error: /main/test_xai_api.script
the Lua module ‘/builtins/scripts/json.lua’ can’t be found
**Also tried dkjson.lua with no luck
The error says it all. There is no such file. Check it yourself in the builtins folder in your project.
Json encoding and decoding is built into the engine.
I reinstalled fresh multiple times and cleared out the cache, deleted build.
I chose a default html5 build.
Don’t see the reference LUA inside the folder.
There is not supposed to be any Lua files. Defold comes with several different APIs that are always available and you never need to require. The JSON library is one. The HTTP lib is another. The go.* functions to manipulate game objects etc
Remove the require and it should work.
Thanks, I gave your response to Grok and they spit out a new script which now works:
local json = require("builtins.scripts.json")
function init(self)
local url = "https://api.x.ai/v1/chat/completions"
local method = "POST"
local api_key = "xai-"
local headers = {
["Authorization"] = "Bearer " .. api_key,
["Content-Type"] = "application/json"
}
local request_body = {
model = "grok-4-latest",
messages = {
{ role = "system", content = "You are a test assistant." },
{ role = "user", content = "Testing. Just say hi and hello world and nothing else." }
},
stream = false,
temperature = 0
}
local post_data = json.encode(request_body)
print("Encoded JSON: " .. post_data)
http.request(url, method, function(self, id, response)
print("Status: " .. response.status)
if response.status == 200 then
local data = json.decode(response.response)
local completion = data.choices[1].message.content
print("Grok Response: " .. completion)
else
local error_msg = response.response and json.decode(response.response) or { message = "No details" }
print("API Test Failed: " .. response.status .. " - " .. (error_msg.message or "Unknown error"))
end
end, headers, post_data)
end