Sample code for 30+ languages & platforms
React Native

WaTrend Send WhatsApp Text

See more WaTrend Examples

Send a WhatsApp text.

Chilkat React Native Downloads

React Native
import { Http, HttpRequest, HttpResponse, JsonObject, 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 actual access token instead of 555555555555555555555555555555
  const req = new HttpRequest();
  req.addParam('number', '84933313xxx');
  req.addParam('type', 'text');
  req.addParam('message', 'This is a test message');
  req.addParam('instance_id', '609ACF283XXXX');
  req.addParam('access_token', '555555555555555555555555555555');

  // Note: The WaTrend online documentation indicate a POST should be used.
  // However, it seems you might actually need to send a GET request.
  // It is unclear.  
  // If a GET is neeed, you would just send to the URL w/ query params like this:
  const sbUrl = new StringBuilder();
  sbUrl.append('https://app.watrend.com/api/send.php?');
  sbUrl.append(req.getUrlEncodedParams());
  const responseBodyStr = await http.quickGetStrAsync(sbUrl.getAsString());
  // The responseBodyStr contains the JSON response from the server..

  req.httpVerb = 'POST';
  req.contentType = 'application/x-www-form-urlencoded';

  const resp = new HttpResponse();
  try {
    await http.httpReqAsync('https://app.watrend.com/api/send.php', req, resp);
  } catch {
    console.log(http.lastErrorText);
    return;
  }

  const sbResponseBody = new StringBuilder();
  resp.getBodySb(sbResponseBody);

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

  console.log(resp.bodyStr);

  // Both success and failed responses use 200 status code.

  // A success response contains this JSON in the response body:
  // {"status":"success", ... }

  // A failed response will contain something like this:
  // {"status":"error","message":"License Invalidated"}

  const jResp = new JsonObject();
  jResp.loadSb(sbResponseBody);

  const status = jResp.stringOf('status');
  console.log(`status: ${status}`);
}