Your First Request

Learn how to make your first API call to validate a license.

Prerequisites

Before making your first request, make sure you have:

  • Created a tenant account
  • Generated an API key (see Authentication)
  • Created a product and plan
  • Created a license and assigned it to a user

The Validate Endpoint

The most common endpoint you'll use

The /api/v1/validate endpoint is the core of the Slice API. It checks whether a user has a valid license and returns the license details if valid.

Endpoint Details

  • Method: POST
  • URL: /api/v1/validate
  • Authentication: Bearer token (API key)
  • Content-Type: application/json

Making the Request

Example request with cURL

Request

curl -X POST https://api.slice.example.com/api/v1/validate \
  -H "Authorization: Bearer sk_live_abc123..." \
  -H "Content-Type: application/json" \
  -d '{
    "userId": "user_123"
  }'

Request Body

{
  "userId": "user_123"  // Your internal user identifier
}

Note: The userId is your internal user identifier. It can be any string that uniquely identifies the user in your system.

Response Examples

What to expect in the response

Valid License Response

{
  "success": true,
  "data": {
    "valid": true,
    "license": {
      "id": "license_abc123",
      "productId": "product_xyz",
      "planId": "plan_456",
      "status": "active",
      "maxSeats": 5,
      "expiresAt": null,
      "features": ["feature1", "feature2"],
      "createdAt": "2024-01-01T00:00:00Z",
      "updatedAt": "2024-01-01T00:00:00Z"
    },
    "activation": {
      "id": "activation_789",
      "userId": "user_internal_id",
      "licenseId": "license_abc123",
      "activatedAt": "2024-01-01T00:00:00Z",
      "lastCheckedAt": "2024-01-15T12:00:00Z"
    },
    "features": ["feature1", "feature2"]
  }
}

Invalid License Response

{
  "success": true,
  "data": {
    "valid": false,
    "reason": "no_license"  // or "expired", "revoked", "suspended", "exceeded_seats"
  }
}

Possible reasons:

  • no_license - User has no assigned license
  • expired - License has expired
  • revoked - License has been revoked
  • suspended - License is suspended
  • exceeded_seats - License has reached its seat limit

Code Examples

Examples in different programming languages

JavaScript/Node.js

async function validateLicense(userId) {
  const response = await fetch('https://api.slice.example.com/api/v1/validate', {
    method: 'POST',
    headers: {
      'Authorization': `Bearer ${process.env.SLICE_API_KEY}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({ userId }),
  });

  const result = await response.json();
  
  if (result.data.valid) {
    console.log('License is valid!');
    console.log('Features:', result.data.features);
    return result.data.license;
  } else {
    console.log('License invalid:', result.data.reason);
    return null;
  }
}

Python

import requests
import os

def validate_license(user_id):
    response = requests.post(
        'https://api.slice.example.com/api/v1/validate',
        headers={
            'Authorization': f'Bearer {os.environ["SLICE_API_KEY"]}',
            'Content-Type': 'application/json',
        },
        json={'userId': user_id}
    )
    
    result = response.json()
    
    if result['data']['valid']:
        print('License is valid!')
        print('Features:', result['data']['features'])
        return result['data']['license']
    else:
        print('License invalid:', result['data']['reason'])
        return None

Using the SDK (Recommended)

import { SliceClient } from '@sliceapi/sdk';

const client = new SliceClient(process.env.SLICE_API_KEY!);

const result = await client.validate.validate('user_123');

if (result.valid) {
  console.log('License is valid!');
  console.log('Features:', result.features);
} else {
  console.log('License invalid:', result.reason);
}

The SDK handles authentication, error handling, and type safety automatically. See the SDK Documentation for more details.

Error Handling

How to handle errors in your requests

HTTP Status Codes

  • 200 OK: Request successful (license may be valid or invalid)
  • 400 Bad Request: Invalid request parameters (e.g., missing userId)
  • 401 Unauthorized: Invalid or missing API key
  • 429 Too Many Requests: Rate limit exceeded
  • 500 Internal Server Error: Server error - retry later

Example Error Response

{
  "success": false,
  "error": "Missing required field: userId"
}

Error Handling Example

try {
  const result = await client.validate.validate('user_123');
  // Handle result...
} catch (error) {
  if (error.statusCode === 401) {
    console.error('Authentication failed - check your API key');
  } else if (error.statusCode === 429) {
    console.error('Rate limit exceeded - retry later');
  } else {
    console.error('Error:', error.message);
  }
}