# Basic Usage

### Steps to Implement

1. **Set Up HTTP Requests**\
   Enable HTTP requests in your game settings:
   * Go to Game Settings > Security
   * Enable "Allow HTTP Requests"
   * Roblox [docs reference](https://create.roblox.com/docs/reference/engine/classes/HttpService#HttpEnabled)
2. **Create a Server Script**
   * In Roblox Studio, create a new Script in ServerScriptService
   * Copy the provided code into this script
3. **Customize API Details**\
   Replace the placeholder values in the script:
   * `<CAMPAIGN_ID>`: Insert your specific campaign ID
   * `<API KEY>`: Insert your provided API key
4. **Error Handling**\
   The script includes basic error handling. You can expand on this by logging errors or implementing retry logic.
5. **Testing**\
   Test the API call in a controlled environment before full implementation.

#### Send Giftoin Function (Lua)

```lua
local HttpService = game:GetService("HttpService")

local function sendGiftoin(apiUrl, apiKey, dataTable)
    local jsonData = HttpService:JSONEncode(dataTable)
    
    local headers = {
        ["Content-Type"] = "application/json",
        ["x-gft-api-key"] = apiKey
    }
    
    local requestDetails = {
        Url = apiUrl,
        Method = "POST",
        Headers = headers,
        Body = jsonData
    }
    
    local success, response = pcall(function()
        return HttpService:RequestAsync(requestDetails)
    end)
    
    if success then
        if response.Success then
            print("Data sent successfully! Response:", response.Body)
            -- Process response data here if needed
            return true, response.Body
        else
            warn("API request failed. Status code:", response.StatusCode)
            return false, response.StatusCode
        end
    else
        warn("Failed to send data: " .. tostring(response))
        return false, response
    end
end

local apiUrl = "https://api.giftoin.org/v1/api-reseller/api/v1/gcm/reseller/campaign/YOUR_CAMPAIGN_ID/order"
local apiKey = "YOUR_API_KEY"

```

### Player Join Event

#### **Triggers when user joins the game**

This API implementation uses Roblox's `HttpService` to send player data to an external endpoint when a a player joins the game. Here's a breakdown of how it works:

1. The script uses the `Players.PlayerAdded` event to detect when a new player joins the game.
2. When a player joins, it creates a data table containing the player's Roblox user ID.
3. The `sendGiftoin` function is called with this data, along with the API URL and API key.
4. The function converts the data to JSON format and sends a POST request to the specified API endpoint.
5. The request includes necessary headers like Content-Type and the API key for authentication.
6. Error handling is implemented using pcall to catch any issues during the HTTP request.
7. The script prints success or failure messages based on the API response.

```lua
game.Players.PlayerAdded:Connect(function(player)
    local dataTable = {
        ["thirdPartyUserIds"] = {
            {["type"] = "roblox",
            ["id"] = tostring(player.UserId)}
        }
    }
    
    local success, result = sendGiftoin(apiUrl, apiKey, dataTable)
    if success then
        print("API call successful for player:", player.Name)
    else
        warn("API call failed for player:", player.Name, "Error:", result)
    end
end)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.giftoin.org/platform-guides/roblox/basic-usage.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
