Sample code for 30+ languages & platforms
Dart 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 Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  // 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.
  final http = CkHttp();

  // 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.basicAuth = true;

  // Make sure to replace "chilkat" with your store name.
  final resp = CkHttpResponse();
  try {
    http.httpNoBody('GET', 'https://chilkat.myshopify.com/admin/products.json', resp);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Examine the response code.
  if (resp.statusCode != 200) {
    print('Received error response code: ${resp.statusCode}');
    print('Response body:');
    print(resp.bodyStr);
    return;
  }

  // Success.
  print('Success.');

  // Examine the JSON response 
  final sbJson = CkStringBuilder();
  resp.getBodySb(sbJson);
  final json = CkJsonObject();
  json.loadSb(sbJson);
  json.emitCompact = false;
  print(json.emit());

  // -------------------------------------------------
  // Now let's do the same using the Chilkat Rest API.
  final rest = CkRest();

  // Provide the private app credentials:
  rest.setAuthBasic('SHOPIFY_PRIVATE_API_KEY', 'SHOPIFY_PRIVATE_API_SECRET_KEY');

  // Connect to the shopify server.
  final bTls = true;
  final port = 443;
  final bAutoReconnect = true;
  // Make sure to replace "chilkat" with your store name.
  try {
    rest.connect('chilkat.myshopify.com', port, bTls, bAutoReconnect);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  sbJson.clear();
  try {
    rest.fullRequestNoBodySb('GET', '/admin/products.json', sbJson);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  if (rest.responseStatusCode != 200) {
    print('Received error response code: ${rest.responseStatusCode}');
    print('Response body:');
    print(sbJson.getAsString());
    return;
  }

  // Success...
  json.loadSb(sbJson);
  print(json.emit());
}