Sample code for 30+ languages & platforms
React Native

Client Certificate in REST (USB Token or Smartcard)

See more REST Examples

Demonstrates how to use a client certificate with a REST connection where the certificate and private key are located on a USB token or smart card.

Chilkat React Native Downloads

React Native
import { Cert, Rest, Socket } from '@chilkat/react-native'

async function chilkatExample() {
  // This example requires the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  // This example shows how to use the Chilkat socket object's connection.
  const rest = new Rest();
  const socket = new Socket();

  // Set the certificate to be used for mutual TLS authentication
  const cert = new Cert();

  // If the smartcard or token requires a PIN...
  cert.smartCardPin = '000000';

  try {
    cert.loadFromSmartcard('');
  } catch {
    console.log(cert.lastErrorText);
    return;
  }

  try {
    socket.setSslClientCert(cert);
  } catch {
    console.log(socket.lastErrorText);
    return;
  }

  // Establish the connection using the socket object (with client certificate authentication).
  const bTls = true;
  const port = 443;
  const maxWaitMs = 5000;
  try {
    await socket.connectAsync('www.example.com', port, bTls, maxWaitMs);
  } catch {
    console.log(`Connect Failure Error Code: ${socket.connectFailReason}`);
    console.log(socket.lastErrorText);
    return;
  }

  const bAutoReconnect = true;

  // Use the connection:
  try {
    rest.useConnection(socket, bAutoReconnect);
  } catch {
    console.log(rest.lastErrorText);
    return;
  }

  // At this point we are connected and can make REST calls...
  // For example..
  let responseJson: string;
  try {
    responseJson = await rest.fullRequestNoBodyAsync('GET', '/someQuery');
  } catch {
    console.log(rest.lastErrorText);
    return;
  }
}