Sample code for 30+ languages & platforms
Delphi DLL

Create Restricted Data Token (RDT)

See more Amazon SP-API Examples

Returns a Restricted Data Token (RDT) for one or more restricted resources that you specify.

Chilkat Delphi DLL Downloads

Delphi DLL
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, AuthAws, Rest, JsonObject, StringBuilder;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
authAws: HCkAuthAws;
rest: HCkRest;
bTls: Boolean;
port: Integer;
bAutoReconnect: Boolean;
jsonToken: HCkJsonObject;
lwa_token: PWideChar;
json: HCkJsonObject;
sbRequest: HCkStringBuilder;
sbResponse: HCkStringBuilder;
uri: PWideChar;
statusCode: Integer;
jsonResp: HCkJsonObject;
expiresIn: Integer;
restrictedDataToken: PWideChar;

begin
success := False;

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

authAws := CkAuthAws_Create();
CkAuthAws_putAccessKey(authAws,'AWS_ACCESS_KEY');
CkAuthAws_putSecretKey(authAws,'AWS_SECRET_KEY');
CkAuthAws_putServiceName(authAws,'execute-api');
// Use the region that is correct for your needs.
CkAuthAws_putRegion(authAws,'eu-west-1');

rest := CkRest_Create();
bTls := True;
port := 443;
bAutoReconnect := True;
// The sandbox endpoint (sandbox.sellingpartnerapi-eu.amazon.com) fails.
// Use the production endpoint and see the note below.
success := CkRest_Connect(rest,'sellingpartnerapi-eu.amazon.com',port,bTls,bAutoReconnect);
if (success = False) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;

success := CkRest_SetAuthAws(rest,authAws);

// Load the previously obtained LWA access token.
// See Fetch SP-API LWA Access Token
jsonToken := CkJsonObject_Create();
success := CkJsonObject_LoadFile(jsonToken,'qa_data/tokens/sp_api_lwa_token.json');
if (success = False) then
  begin
    Memo1.Lines.Add('Failed to load LWA access token.');
    Exit;
  end;

// Add the x-amz-access-token request header.
lwa_token := CkJsonObject__stringOf(jsonToken,'access_token');
CkRest_ClearAllHeaders(rest);
CkRest_AddHeader(rest,'x-amz-access-token',lwa_token);

// We're going to send a POST with the following JSON body:

// {
//   "restrictedResources": [
//     {
//       "method": "GET",
//       "path": "/orders/v0/orders",
//       "dataElements": ["buyerInfo", "shippingAddress"]
//     }
//   ]
// }

// Use this online tool to generate code from sample JSON: 
// Generate Code to Create JSON

json := CkJsonObject_Create();
CkJsonObject_UpdateString(json,'restrictedResources[0].method','GET');
CkJsonObject_UpdateString(json,'restrictedResources[0].path','/orders/v0/orders');
CkJsonObject_UpdateString(json,'restrictedResources[0].dataElements[0]','buyerInfo');
CkJsonObject_UpdateString(json,'restrictedResources[0].dataElements[1]','shippingAddress');

sbRequest := CkStringBuilder_Create();
CkJsonObject_EmitSb(json,sbRequest);

sbResponse := CkStringBuilder_Create();
uri := '/tokens/2021-03-01/restrictedDataToken';
success := CkRest_FullRequestSb(rest,'POST',uri,sbRequest,sbResponse);
if (success = False) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;

// ------------------------------------------------------------------------------------
// Note: Using the sandbox endpoint, such as sandbox.sellingpartnerapi-eu.amazon.com
// results in a response status code of 400 with the following error:
// [{"code":"InvalidInput","message":"Could not match input arguments"}]
// Getting a restricted data token seems to only work with the production endpoint.
// ------------------------------------------------------------------------------------

// Examine the response status.
statusCode := CkRest_getResponseStatusCode(rest);
if (statusCode <> 200) then
  begin
    Memo1.Lines.Add('Response status code: ' + IntToStr(statusCode));
    Memo1.Lines.Add('Response status text: ' + CkRest__responseStatusText(rest));
    Memo1.Lines.Add('Response body: ');
    Memo1.Lines.Add(CkStringBuilder__getAsString(sbResponse));
    Memo1.Lines.Add('Failed.');
    Exit;
  end;

Memo1.Lines.Add(CkStringBuilder__getAsString(sbResponse));

// If successful, gets a JSON response such as the following:

// {
//   "expiresIn": 3600,
//   "restrictedDataToken": "Atz.sprdt|AYAB.....TQ="
// }

// Use this online tool to generate parsing code from sample JSON: 
// Generate Parsing Code from JSON

jsonResp := CkJsonObject_Create();
CkJsonObject_LoadSb(jsonResp,sbResponse);

expiresIn := CkJsonObject_IntOf(jsonResp,'expiresIn');
restrictedDataToken := CkJsonObject__stringOf(jsonResp,'restrictedDataToken');

// Save the RDT for subsequent use..
success := CkJsonObject_WriteFile(jsonResp,'qa_data/tokens/sp_api_rdt_token.json');

Memo1.Lines.Add('Success!');

CkAuthAws_Dispose(authAws);
CkRest_Dispose(rest);
CkJsonObject_Dispose(jsonToken);
CkJsonObject_Dispose(json);
CkStringBuilder_Dispose(sbRequest);
CkStringBuilder_Dispose(sbResponse);
CkJsonObject_Dispose(jsonResp);

end;