DocsOMobileSDK Quick Start
Getting Started

SDK Quick Start

OMobileorravo.com/docs/omobile/sdk-quick-start

React Native / Expo

javascriptimport * as SecureStore from 'expo-secure-store';
import { Platform } from 'react-native';
import Constants from 'expo-constants';

const BASE_URL = 'https://yoursite.com/wp-json/omobile/v1/';
const INSTALL_ID = Constants.installationId; // stable UUID per install

async function authHeaders() {
  const token = await SecureStore.getItemAsync('access_token');
  return {
    'Authorization': `Bearer ${token}`,
    'X-Om-Install-Id': INSTALL_ID,
    'Content-Type': 'application/json',
  };
}

// Login
export async function login(username, password) {
  const res = await fetch(`${BASE_URL}auth/login`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'X-Om-Install-Id': INSTALL_ID,
    },
    body: JSON.stringify({ username, password }),
  });
  const data = await res.json();
  if (data.access_token) {
    await SecureStore.setItemAsync('access_token', data.access_token);
    await SecureStore.setItemAsync('refresh_token', data.refresh_token);
  }
  return data;
}

// Refresh token
export async function refreshToken() {
  const refresh = await SecureStore.getItemAsync('refresh_token');
  const res = await fetch(`${BASE_URL}auth/refresh`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ refresh_token: refresh }),
  });
  const data = await res.json();
  if (data.access_token) {
    await SecureStore.setItemAsync('access_token', data.access_token);
    await SecureStore.setItemAsync('refresh_token', data.refresh_token);
  }
  return data;
}

// Get config (flags + remote config)
export async function getConfig() {
  const res = await fetch(
    `${BASE_URL}config?platform=${Platform.OS}&version=${Constants.expoConfig.version}`,
    { headers: await authHeaders() }
  );
  return res.json();
}

// Register device + push token
export async function registerDevice(pushToken) {
  const res = await fetch(`${BASE_URL}devices/register`, {
    method: 'POST',
    headers: await authHeaders(),
    body: JSON.stringify({
      platform: Platform.OS,
      app_version: Constants.expoConfig.version,
      push_token: pushToken,
      locale: 'en-US',
      timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
    }),
  });
  return res.json();
}

// Submit telemetry events
export async function trackEvents(events) {
  await fetch(`${BASE_URL}telemetry/events`, {
    method: 'POST',
    headers: await authHeaders(),
    body: JSON.stringify({ events }),
  });
}

// Report crash
export async function reportCrash(error) {
  await fetch(`${BASE_URL}crashes/report`, {
    method: 'POST',
    headers: await authHeaders(),
    body: JSON.stringify({
      message: error.message,
      stack_trace: error.stack,
      platform: Platform.OS,
      app_version: Constants.expoConfig.version,
      os_version: Platform.Version.toString(),
    }),
  });
}

Flutter

dartimport 'package:http/http.dart' as http;
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'dart:convert';
import 'dart:io';

const baseUrl = 'https://yoursite.com/wp-json/omobile/v1/';
final _storage = FlutterSecureStorage();
const installId = 'YOUR-UUID-HERE'; // generate once, store persistently

Future<Map<String, String>> authHeaders() async {
  final token = await _storage.read(key: 'access_token') ?? '';
  return {
    'Authorization': 'Bearer $token',
    'X-Om-Install-Id': installId,
    'Content-Type': 'application/json',
  };
}

// Login
Future<Map<String, dynamic>> login(String username, String password) async {
  final res = await http.post(
    Uri.parse('${baseUrl}auth/login'),
    headers: {
      'Content-Type': 'application/json',
      'X-Om-Install-Id': installId,
    },
    body: jsonEncode({'username': username, 'password': password}),
  );
  final data = jsonDecode(res.body) as Map<String, dynamic>;
  if (data['access_token'] != null) {
    await _storage.write(key: 'access_token', value: data['access_token']);
    await _storage.write(key: 'refresh_token', value: data['refresh_token']);
  }
  return data;
}

// Get config
Future<Map<String, dynamic>> getConfig(String version) async {
  final platform = Platform.isIOS ? 'ios' : 'android';
  final res = await http.get(
    Uri.parse('${baseUrl}config?platform=$platform&version=$version'),
    headers: await authHeaders(),
  );
  return jsonDecode(res.body);
}

// Register device
Future<void> registerDevice(String pushToken, String version) async {
  final platform = Platform.isIOS ? 'ios' : 'android';
  await http.post(
    Uri.parse('${baseUrl}devices/register'),
    headers: await authHeaders(),
    body: jsonEncode({
      'platform': platform,
      'app_version': version,
      'push_token': pushToken,
    }),
  );
}

// Track events
Future<void> trackEvents(List<Map<String, dynamic>> events) async {
  await http.post(
    Uri.parse('${baseUrl}telemetry/events'),
    headers: await authHeaders(),
    body: jsonEncode({'events': events}),
  );
}

// Report crash
Future<void> reportCrash(Object error, StackTrace stack) async {
  final platform = Platform.isIOS ? 'ios' : 'android';
  await http.post(
    Uri.parse('${baseUrl}crashes/report'),
    headers: await authHeaders(),
    body: jsonEncode({
      'message': error.toString(),
      'stack_trace': stack.toString(),
      'platform': platform,
    }),
  );
}

SDK Quick Start — OMobile Docs — Orravo