Making Authenticated Requests

Use this in the Header of every request you send to Violet.

Required Headers for API Requests

X-Violet-App-Id: your-app-id-here
X-Violet-App-Secret: your-app-secret-here
X-Violet-Token: your-authentication-token-here
Content-Type: application/json

Example API Request

async function makeVioletAPIRequest(authToken, endpoint, method = 'GET', data = null) {
  const url = `https://sandbox-api.violet.io/v1${endpoint}`;
  const options = {
    method,
    headers: {
      'X-Violet-App-Id': process.env.VIOLET_APP_ID,
      'X-Violet-App-Secret': process.env.VIOLET_APP_SECRET,
      'X-Violet-Token': authToken,
      'Content-Type': 'application/json'
    }
  };

  if (data && (method === 'POST' || method === 'PUT' || method === 'PATCH')) {
    options.body = JSON.stringify(data);
  }

  try {
    const response = await fetch(url, options);

    if (response.status === 401) {
      throw new Error('TOKEN_EXPIRED');
    }

    if (!response.ok) {
      const errorData = await response.json();
      throw new Error(errorData.message || 'API request failed');
    }

    return response.json();
  } catch (error) {
    throw error;
  }
}

// Example usage
const merchants = await makeVioletAPIRequest(authToken, '/merchants');

Last updated

Was this helpful?