Sample code for 30+ languages & platforms
Lua

Create a New GMail Label

See more GMail REST API Examples

Demonstrates how to create a new GMail label.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.

local http = chilkat.Http.new()
http.auth_token = "GMAIL-ACCESS-TOKEN"

local userId = "me"
http:set_url_var("userId", userId)

-- Create the JSON to be sent in the HTTP request body.
-- The name of the new label is "questions".
local json = chilkat.JsonObject.new()
json:update_string("name", "questions")
json:update_string("labelListVisibility", "labelShow")
json:update_string("messageListVisibility", "show")

json.emit_compact = false
print(json:emit())

-- The JSON contains this:
-- {
--   "name": "questions",
--   "labelListVisibility": "labelShow",
--   "messageListVisibility": "show"
-- }

local url = "https://www.googleapis.com/gmail/v1/users/{$userId}/labels"
local resp = chilkat.HttpResponse.new()
local success = http:http_json("POST", url, json, "application/json", resp)
if not success then
    print(http.last_error_text)
    return
end

print("status = " .. resp.status_code)

-- A 200 response status indicate success.
if resp.status_code ~= 200 then
    print(resp.body_str)
    print("Failed.")
    return
end

-- A successful repsonse contains JSON that looks like this:

-- {
--  "id": "Label_43",
--  "name": "questions",
--  "messageListVisibility": "show",
--  "labelListVisibility": "labelShow"
-- }

print("response body:")
print(resp.body_str)

print("GMail label created!")