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

Shopify Private Authentication for Private Apps

See more Shopify Examples

Shopify private authentication is for interacting with your own store through private applications. It uses HTTP "Basic" authentication with your Shopify private application key and secret key.

This example demonstrates how to send a private authenticated request using Chilkat Http, and then the same using Chilkat Rest.

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.

-- First demonstrate sending a simple request using Shopify private authentication w/ the Chilkat Http API.
local http = chilkat.Http.new()

-- To use HTTP Basic Authentication with any HTTP request, we simply set the Login, Password, and BasicAuth properties.
-- Important: All HTTP requests using Basic authentication must be over SSL/TLS.
http.login = "SHOPIFY_PRIVATE_API_KEY"
http.password = "SHOPIFY_PRIVATE_API_SECRET_KEY"
http.basic_auth = true

-- Make sure to replace "chilkat" with your store name.
local resp = chilkat.HttpResponse.new()
local success = http:http_no_body("GET", "https://chilkat.myshopify.com/admin/products.json", resp)
if not success then
    print(http.last_error_text)
    return
end

-- Examine the response code.
if resp.status_code ~= 200 then
    print("Received error response code: " .. resp.status_code)
    print("Response body:")
    print(resp.body_str)
    return
end

-- Success.
print("Success.")

-- Examine the JSON response
local sbJson = chilkat.StringBuilder.new()
resp:get_body_sb(sbJson)
local json = chilkat.JsonObject.new()
json:load_sb(sbJson)
json.emit_compact = false
print(json:emit())

-- -------------------------------------------------
-- Now let's do the same using the Chilkat Rest API.
local rest = chilkat.Rest.new()

-- Provide the private app credentials:
rest:set_auth_basic("SHOPIFY_PRIVATE_API_KEY", "SHOPIFY_PRIVATE_API_SECRET_KEY")

-- Connect to the shopify server.
local bTls = true
local port = 443
local bAutoReconnect = true
-- Make sure to replace "chilkat" with your store name.
success = rest:connect("chilkat.myshopify.com", port, bTls, bAutoReconnect)
if not success then
    print(rest.last_error_text)
    return
end

sbJson:clear()
success = rest:full_request_no_body_sb("GET", "/admin/products.json", sbJson)
if not rest.last_method_success then
    print(rest.last_error_text)
    return
end

if rest.response_status_code ~= 200 then
    print("Received error response code: " .. rest.response_status_code)
    print("Response body:")
    print(sbJson:get_as_string())
    return
end

-- Success...
json:load_sb(sbJson)
print(json:emit())