Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.6.0+

Azure Table: Insert an Entity with Typed Properties

See more Azure Table Service Examples

Insert into an existing table using Prefer: return-no-content. Requires HTTP 204, avoids parsing its empty body, and captures the ETag for later conditional writes. Insert is create-only; an existing PartitionKey/RowKey pair produces a conflict.

Includes explicit Edm.Guid, Edm.DateTime, and Edm.Int64 annotations. The Int64 is serialized as a string beyond JavaScript's exact integer range, preserving its digits in every generated language. The timestamp is server-managed. AmountDue demonstrates Edm.Double, which is not exact decimal currency. See Table JSON types.

Uses Chilkat.Rest and AuthAzureStorage with Table-specific Shared Key signing. Load a private qa_data/azure/storage.json containing string fields account and access_key (the base64 account key, not a connection string or SAS). Keep this file outside public web content and source control. Targets Azure Storage Table service in the public cloud, not Azure Cosmos DB for Table.

Pins x-ms-version: 2026-04-06, Microsoft's documented fully deployed Storage version at review time, and sends OData 3.0 headers. Older supported versions are not automatically obsolete. See Storage versioning and OData headers.

Microsoft recommends Microsoft Entra ID authorization, preferably managed identity where available. This example retains its existing Shared Key authentication pattern; use it only when account policy permits. TLS certificates are verified, timeouts are bounded, and diagnostic dumps and entity payloads are not printed. A lost response can leave a write's outcome uncertain: inspect the target before retrying.

Microsoft insert entity reference.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- StringBuilder compares text by value in every generated language.
local textCompare = chilkat.StringBuilder.new()
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
local success = false
local config = chilkat.JsonObject.new()
config.strict = true
success = config:load_file("qa_data/azure/storage.json")
if not success then
    print("Could not load private Azure configuration.")
    return
end

textCompare:clear()
textCompare:set_string(config:string_of("account"))
local sameText1 = textCompare:contents_equal("", true)
if (config:json_type_of("account") ~= 1) or (sameText1) then
    print("Supply the storage account name.")
    return
end

-- access_key is the base64 account key, not a connection string or SAS.
textCompare:clear()
textCompare:set_string(config:string_of("access_key"))
local sameText2 = textCompare:contents_equal("", true)
if (config:json_type_of("access_key") ~= 1) or (sameText2) then
    print("Supply the base64 account access key.")
    return
end

local accountText = chilkat.StringBuilder.new()
success = accountText:append(config:string_of("account"))
if not success then
    print("Could not prepare the request.")
    return
end

if (accountText.length < 3) or (accountText.length > 24) then
    print("Storage account names need 3-24 lowercase letters or digits.")
    return
end

local allowedAccountChars = chilkat.StringBuilder.new()
success = allowedAccountChars:append("abcdefghijklmnopqrstuvwxyz0123456789")
if not success then
    print("Could not prepare the request.")
    return
end

local charIndex = 0
while charIndex < accountText.length do
    if not allowedAccountChars:contains(accountText:get_range(charIndex, 1, false), true) then
        print("Storage account names need lowercase letters or digits.")
        return
    end

    charIndex = charIndex + 1
end

local endpoint = chilkat.StringBuilder.new()
success = endpoint:append(config:string_of("account"))
if not success then
    print("Could not prepare the request.")
    return
end

success = endpoint:append(".table.core.windows.net")
if not success then
    print("Could not prepare the request.")
    return
end

-- Table names contain only letters and digits and are case-insensitive.
-- Entity PartitionKey and RowKey values are case-sensitive.
local resourcePath = "/ExampleCustomers"
local rest = chilkat.Rest.new()
rest.connect_timeout_ms = 20000
rest.idle_timeout_ms = 60000
local azAuth = chilkat.AuthAzureStorage.new()
azAuth.account = config:string_of("account")
azAuth.access_key = config:string_of("access_key")
azAuth.service = "Table"
azAuth.scheme = "SharedKey"
azAuth.xms_version = "2026-04-06"
success = rest:set_auth_azure_storage(azAuth)
if not success then
    print("Could not configure Shared Key signing.")
    return
end

local connection = chilkat.Socket.new()
connection.require_ssl_cert_verify = true
connection.max_read_idle_ms = 60000
connection.max_send_idle_ms = 60000
success = connection:connect(endpoint:get_as_string(), 443, true, 20000)
if not success then
    print("TLS connection failed; inspect diagnostics privately.")
    return
end

-- Do not replay a write automatically after losing its response.
success = rest:use_connection(connection, false)
if not success then
    print("Could not prepare the request.")
    return
end

local responseBody = chilkat.StringBuilder.new()
success = rest:add_header("DataServiceVersion", "3.0;NetFx")
if not success then
    print("Could not prepare the request.")
    return
end

success = rest:add_header("MaxDataServiceVersion", "3.0;NetFx")
if not success then
    print("Could not prepare the request.")
    return
end

success = rest:add_header("Content-Type", "application/json")
if not success then
    print("Could not prepare the request.")
    return
end

success = rest:add_header("Accept", "application/json;odata=minimalmetadata")
if not success then
    print("Could not prepare the request.")
    return
end

success = rest:add_header("Prefer", "return-no-content")
if not success then
    print("Could not prepare the request.")
    return
end

-- The table must already exist. Change resourcePath above to your table's name.
-- PartitionKey + RowKey uniquely identify the entity. Inserting an existing pair returns 409.
-- Adapt these fixed sample values; keys cannot contain /, backslash, #, ?, or control characters.
-- Timestamp is server-managed; do not put it in the request.
local json = chilkat.JsonObject.new()
success = json:update_string("PartitionKey", "customers-west")
if not success then
    print("Could not prepare the request.")
    return
end

success = json:update_string("RowKey", "customer-001")
if not success then
    print("Could not prepare the request.")
    return
end

success = json:update_string("Address", "Mountain View")
if not success then
    print("Could not prepare the request.")
    return
end

success = json:update_int("Age", 23)
if not success then
    print("Could not prepare the request.")
    return
end

success = json:update_number("AmountDue", "200.23")
if not success then
    print("Could not prepare the request.")
    return
end

-- AmountDue is Edm.Double, not exact decimal currency. Use an integer minor-unit design for exact money.
-- Quote the whole JSON path so @odata.type is a literal property name.
success = json:update_string("\"CustomerCode@odata.type\"", "Edm.Guid")
if not success then
    print("Could not prepare the request.")
    return
end

success = json:update_string("CustomerCode", "c9da6455-213d-42c9-9a79-3e9149a57833")
if not success then
    print("Could not prepare the request.")
    return
end

-- Quote the whole JSON path so @odata.type is a literal property name.
success = json:update_string("\"CustomerSince@odata.type\"", "Edm.DateTime")
if not success then
    print("Could not prepare the request.")
    return
end

success = json:update_string("CustomerSince", "2008-07-10T00:00:00Z")
if not success then
    print("Could not prepare the request.")
    return
end

-- Quote the whole JSON path so @odata.type is a literal property name.
success = json:update_string("\"NumberOfOrders@odata.type\"", "Edm.Int64")
if not success then
    print("Could not prepare the request.")
    return
end

success = json:update_string("NumberOfOrders", "9007199254740993")
if not success then
    print("Could not prepare the request.")
    return
end

success = json:update_bool("IsActive", true)
if not success then
    print("Could not prepare the request.")
    return
end

local requestBody = chilkat.StringBuilder.new()
success = json:emit_sb(requestBody)
if not success then
    print("Could not serialize the JSON request.")
    return
end

success = rest:full_request_sb("POST", resourcePath, requestBody, responseBody)
if not success then
    print("Request did not complete; check the target before retrying.")
    return
end

if rest.response_status_code ~= 204 then
    print("HTTP status: " .. rest.response_status_code)
    print("Request ID: " .. rest:response_hdr_by_name("x-ms-request-id"))
    -- A conflict is not success. Do not replay or convert this request into an upsert.
    if rest.response_status_code == 409 then
        print("The table/entity may already exist, or a table may still be deleting. Inspect its state.")
    end

    -- Table errors may be in an OData JSON body instead of x-ms-error-code.
    -- Keep the body and LastErrorText private; they can contain entity data.
    return
end

-- HTTP 204 has no JSON body. Preserve this ETag exactly, including W/ and quotes,
-- for a later conditional update or delete with If-Match.
local entityEtag = rest:response_hdr_by_name("ETag")
textCompare:clear()
textCompare:set_string(entityEtag)
local sameText3 = textCompare:contents_equal("", true)
if sameText3 then
    print("Entity inserted, but ETag is missing. Retrieve the entity before a conditional write; do not repeat the insert.")
    return
end

print("Entity inserted.")
print("ETag: " .. entityEtag)