Sample code for 30+ languages & platforms
React Native

WordPress API Key Authentication with miniOrange Plugin

See more WordPress Examples

Demonstrates API key authentication using the miniOrange API Authentication plugin.

Chilkat React Native Downloads

React Native
import { DtObj, Http, JsonArray, StringBuilder } from '@chilkat/react-native'

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

  const http = new Http();

  // Use your API key here, such as TaVFjSBu8IMR0MbvZNn7A6P04GXrbtHm
  // This causes the "Authorization: Bearer <api_key>" header to be added to each HTTP request.
  http.authToken = 'api_key';

  const sbResponseBody = new StringBuilder();
  try {
    await http.quickGetSbAsync('https://www.yoursite.com/wp-json/wp/v2/posts?page=1', sbResponseBody);
  } catch {
    console.log(http.lastErrorText);
    return;
  }

  const jarrResp = new JsonArray();
  jarrResp.loadSb(sbResponseBody);
  jarrResp.emitCompact = false;

  console.log('Response Body:');
  console.log(jarrResp.emit());

  const respStatusCode = http.lastStatus;
  console.log(`Response Status Code = ${respStatusCode}`);
  if (respStatusCode >= 400) {
    console.log('Response Header:');
    console.log(http.lastHeader);
    console.log('Failed.');
    return;
  }

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

  // [
  //   {
  //     "id": 1902,
  //     "date": "2020-11-16T09:54:09",
  //     "date_gmt": "2020-11-16T16:54:09",
  //     "guid": {
  //       "rendered": "http:\/\/cknotes.com\/?p=1902"
  //     },
  //     "modified": "2020-11-16T09:54:09",
  //     "modified_gmt": "2020-11-16T16:54:09",
  //     "slug": "xero-redirect-uri-for-oauth2-and-desktop-apps",
  //     "status": "publish",
  //     "type": "post",
  //     "link": "https:\/\/cknotes.com\/xero-redirect-uri-for-oauth2-and-desktop-apps\/",
  //     "title": {
  //       "rendered": "Xero Redirect URI for OAuth2 and Desktop Apps"
  //     },
  //     "content": {
  //       "rendered": "<p>...",
  //       "protected": false
  //     },
  //     "excerpt": {
  //       "rendered": "<p>...",
  //       "protected": false
  //     },
  //     "author": 1,
  //     "featured_media": 0,
  //     "comment_status": "closed",
  //     "ping_status": "open",
  //     "sticky": false,
  //     "template": "",
  //     "format": "standard",
  //     "meta": [
  //     ],
  //     "categories": [
  //       815
  //     ],
  //     "tags": [
  //       594,
  //       816
  //     ],
  //     "_links": {
  //       "self": [
  //         {
  //           "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/posts\/1902"
  //         }
  //       ],
  //       "collection": [
  //         {
  //           "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/posts"
  //         }
  //       ],
  //       "about": [
  //         {
  //           "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/types\/post"
  //         }
  //       ],
  //       "author": [
  //         {
  //           "embeddable": true,
  //           "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/users\/1"
  //         }
  //       ],
  //       "replies": [
  //         {
  //           "embeddable": true,
  //           "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/comments?post=1902"
  //         }
  //       ],
  //       "version-history": [
  //         {
  //           "count": 1,
  //           "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/posts\/1902\/revisions"
  //         }
  //       ],
  //       "predecessor-version": [
  //         {
  //           "id": 1904,
  //           "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/posts\/1902\/revisions\/1904"
  //         }
  //       ],
  //       "wp:attachment": [
  //         {
  //           "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/media?parent=1902"
  //         }
  //       ],
  //       "wp:term": [
  //         {
  //           "taxonomy": "category",
  //           "embeddable": true,
  //           "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/categories?post=1902"
  //         },
  //         {
  //           "taxonomy": "post_tag",
  //           "embeddable": true,
  //           "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/tags?post=1902"
  //         }
  //       ],
  //       "curies": [
  //         {
  //           "name": "wp",
  //           "href": "https:\/\/api.w.org\/{rel}",
  //           "templated": true
  //         }
  //       ]
  //     }
  //   },
  // ...
  // ]

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

  const dateGmt = new DtObj();
  let id = 0;
  let date = '';
  let guidRendered = '';
  let modified = '';
  let modifiedGmt = '';
  let slug = '';
  let status = '';
  let vType = '';
  let link = '';
  let titleRendered = '';
  let contentRendered = '';
  let excerptRendered = '';
  let author = 0;
  let featuredMedia = 0;
  let commentStatus = '';
  let pingStatus = '';
  let templateArg = '';
  let format = '';
  let j = 0;
  let countJ = 0;
  let intVal = 0;
  let href = '';
  let count = 0;
  let taxonomy = '';
  let name = '';

  let i = 0;
  const countI = jarrResp.size;
  while (i < countI) {
    const json = jarrResp.objectAt(i);
    id = json.intOf('id');
    date = json.stringOf('date');
    json.dtOf('date_gmt', false, dateGmt);
    guidRendered = json.stringOf('guid.rendered');
    modified = json.stringOf('modified');
    modifiedGmt = json.stringOf('modified_gmt');
    slug = json.stringOf('slug');
    status = json.stringOf('status');
    vType = json.stringOf('type');
    link = json.stringOf('link');
    titleRendered = json.stringOf('title.rendered');
    contentRendered = json.stringOf('content.rendered');
    json.boolOf('content.protected');
    excerptRendered = json.stringOf('excerpt.rendered');
    json.boolOf('excerpt.protected');
    author = json.intOf('author');
    featuredMedia = json.intOf('featured_media');
    commentStatus = json.stringOf('comment_status');
    pingStatus = json.stringOf('ping_status');
    json.boolOf('sticky');
    templateArg = json.stringOf('template');
    format = json.stringOf('format');
    j = 0;
    countJ = json.sizeOfArray('meta');
    while (j < countJ) {
      json.j = j;
      j++;
    }

    j = 0;
    countJ = json.sizeOfArray('categories');
    while (j < countJ) {
      json.j = j;
      intVal = json.intOf('categories[j]');
      j++;
    }

    j = 0;
    countJ = json.sizeOfArray('tags');
    while (j < countJ) {
      json.j = j;
      intVal = json.intOf('tags[j]');
      j++;
    }

    j = 0;
    countJ = json.sizeOfArray('_links.self');
    while (j < countJ) {
      json.j = j;
      href = json.stringOf('_links.self[j].href');
      j++;
    }

    j = 0;
    countJ = json.sizeOfArray('_links.collection');
    while (j < countJ) {
      json.j = j;
      href = json.stringOf('_links.collection[j].href');
      j++;
    }

    j = 0;
    countJ = json.sizeOfArray('_links.about');
    while (j < countJ) {
      json.j = j;
      href = json.stringOf('_links.about[j].href');
      j++;
    }

    j = 0;
    countJ = json.sizeOfArray('_links.author');
    while (j < countJ) {
      json.j = j;
      json.boolOf('_links.author[j].embeddable');
      href = json.stringOf('_links.author[j].href');
      j++;
    }

    j = 0;
    countJ = json.sizeOfArray('_links.replies');
    while (j < countJ) {
      json.j = j;
      json.boolOf('_links.replies[j].embeddable');
      href = json.stringOf('_links.replies[j].href');
      j++;
    }

    j = 0;
    countJ = json.sizeOfArray('_links.version-history');
    while (j < countJ) {
      json.j = j;
      count = json.intOf('_links.version-history[j].count');
      href = json.stringOf('_links.version-history[j].href');
      j++;
    }

    j = 0;
    countJ = json.sizeOfArray('_links.predecessor-version');
    while (j < countJ) {
      json.j = j;
      id = json.intOf('_links.predecessor-version[j].id');
      href = json.stringOf('_links.predecessor-version[j].href');
      j++;
    }

    j = 0;
    countJ = json.sizeOfArray('_links.wp:attachment');
    while (j < countJ) {
      json.j = j;
      href = json.stringOf('_links.wp:attachment[j].href');
      j++;
    }

    j = 0;
    countJ = json.sizeOfArray('_links.wp:term');
    while (j < countJ) {
      json.j = j;
      taxonomy = json.stringOf('_links.wp:term[j].taxonomy');
      json.boolOf('_links.wp:term[j].embeddable');
      href = json.stringOf('_links.wp:term[j].href');
      j++;
    }

    j = 0;
    countJ = json.sizeOfArray('_links.curies');
    while (j < countJ) {
      json.j = j;
      name = json.stringOf('_links.curies[j].name');
      href = json.stringOf('_links.curies[j].href');
      json.boolOf('_links.curies[j].templated');
      j++;
    }

    i++;
  }
}