AI Text Generation in ROBLOX; (Google Gemini API)

Steps :
-Go to your game’s security settings and switch Allow HTTP Requests to on
-Replace the apiKey with your Gemini API key; Get your Gemini API key here (must be 18+ and be in availible countries, you’ll be redirected here if you’re not eligible)
-Change prompt string to your prompt
-Do whatever you want with msgfinal as it’s what Gemini retuened

Recommendation :
-I recommend filtering the results using TextService:FilterStringAsync() as you can’t guarantee the results are up to ROBLOX TOS standards.
-Get trustworthy developers as you have to trust your developers with your Gemini API key.

Code down below :

local HttpService = game:GetService("HttpService")

local apiKey = "insert ur api key here"
local url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key="..apiKey

local prompt = "insert ur prompt here"
local requestBody = HttpService:JSONEncode({
	contents = {
		{
			parts = {
				{ text = prompt }
			}
		}
	}
})

local success, response = pcall(function()
	return HttpService:PostAsync(url, requestBody, Enum.HttpContentType.ApplicationJson, false)
end)

-- Handle the response
if success then
	local decodedResponse = HttpService:JSONDecode(response)
	
	local msgfinal
	
	for index,text in pairs(decodedResponse["candidates"][1]["content"]["parts"][1]) do
		msgfinal = text
	end
	
	print(msgfinal)
else
	print("error: "..response)
end
4 Likes

You could just use the new Secret API to hide your API key securely

3 Likes

I’ve got a question, having not used HTTP service much before and considering the pings to external sources, how long does it take to recieve the response? Is it something to be improved in the future and not yet usable in real time or is it regular latency for real time usage?

for me it took about 5 seconds to generate 4 paragraphs of text so it’s pretty fast

Wait what’s so bad about sharing my API key? Is it ok to share it with my friends, if I literally generated it today?

a lot of bad things can happen when you share your key, someone can use it for malicious purposes and get you banned. (for example: hate speech)