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

Shopify GraphQL Simple Query (Get Shop Object)

See more Shopify Examples

Demonstrates a simple Shopify GraphQL query to get specific fields of the Shop object.

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()

-- This example will use private authentication (which is HTTP Basic authentication)
-- See the other Chilkat Shopify examples for OAuth2 authentication.
-- 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

-- We're going to do a POST  https://{shop}.myshopify.com/admin/api/2021-04/graphql.json
-- Make sure to replace "chilkat" with your store name.

-- The body of the request will be:
--    {
--        shop {
--            id
--            name
--            description
--            email
--         }
--     }

-- The above query is not JSON.  It looks like JSON, but it's actually not.
-- We'll just make it one line:

local query = "{ shop { id name description email } }"

-- My store name is "chilkat".  Use your store name here instead.
local url = "https://chilkat.myshopify.com/admin/api/2021-04/graphql.json"

local resp = chilkat.HttpResponse.new()
local success = http:http_str("POST", url, query, "utf-8", "application/graphql", 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

local sbResponseBody = chilkat.StringBuilder.new()
resp:get_body_sb(sbResponseBody)
local jResp = chilkat.JsonObject.new()
jResp:load_sb(sbResponseBody)
jResp.emit_compact = false

print("Response Body:")
print(jResp:emit())

local respStatusCode = resp.status_code
print("Response Status Code = " .. respStatusCode)
if respStatusCode >= 400 then
    print("Response Header:")
    print(resp.header)
    print("Failed.")
    return
end

-- Sample JSON response:
-- (Sample code for parsing the JSON response is shown below)

-- {
--   "data": {
--     "shop": {
--       "id": "gid:\/\/shopify\/Shop\/24198053",
--       "name": "chilkat",
--       "description": null,
--       "email": "admin@chilkatsoft.com"
--     }
--   },
--   "extensions": {
--     "cost": {
--       "requestedQueryCost": 1,
--       "actualQueryCost": 1,
--       "throttleStatus": {
--         "maximumAvailable": 1000.0,
--         "currentlyAvailable": 999,
--         "restoreRate": 50.0
--       }
--     }
--   }
-- }

-- Sample code for parsing the JSON response...
-- Use the following online tool to generate parsing code from sample JSON:
-- Generate Parsing Code from JSON

local shopId = jResp:string_of("data.shop.id")
local shopName = jResp:string_of("data.shop.name")
local shopDescription = jResp:string_of("data.shop.description")
local shopEmail = jResp:string_of("data.shop.email")
local costRequestedQueryCost = jResp:int_of("extensions.cost.requestedQueryCost")
local costActualQueryCost = jResp:int_of("extensions.cost.actualQueryCost")
local costThrottleStatusMaximumAvailable = jResp:string_of("extensions.cost.throttleStatus.maximumAvailable")
local costThrottleStatusCurrentlyAvailable = jResp:int_of("extensions.cost.throttleStatus.currentlyAvailable")
local costThrottleStatusRestoreRate = jResp:string_of("extensions.cost.throttleStatus.restoreRate")

print("Shop name: " .. shopName)
-- ...