OpenCoreDev Releases Domain SDK 0.2.0: One TypeScript API to Add, Verify, and Remove Customer Domains Across Five Platforms
Back to Tutorials
toolsTutorialintermediate

OpenCoreDev Releases Domain SDK 0.2.0: One TypeScript API to Add, Verify, and Remove Customer Domains Across Five Platforms

July 14, 20261 views4 min read

Learn to use OpenCoreDev's Domain SDK 0.2.0 to manage customer domains across five hosting platforms using a single TypeScript API.

Introduction

In this tutorial, you'll learn how to use OpenCoreDev's Domain SDK 0.2.0 to manage customer domains across multiple hosting platforms using a single TypeScript API. This SDK simplifies domain lifecycle management by abstracting the complexity of working with different platforms like Vercel, Cloudflare, Railway, Render, and Netlify. You'll build a practical domain management system that can add, verify, and remove domains programmatically.

Prerequisites

  • Basic knowledge of TypeScript and Node.js
  • Node.js installed (version 16 or higher)
  • Access credentials for at least one of the supported platforms (Vercel, Cloudflare, Railway, Render, or Netlify)
  • npm or yarn package manager

Step-by-Step Instructions

1. Initialize Your Project

1.1 Create a new Node.js project

First, create a new directory for your project and initialize it with npm:

mkdir domain-manager
 cd domain-manager
npm init -y

1.2 Install required dependencies

Install the Domain SDK and TypeScript dependencies:

npm install @opencoredev/domain-sdk
npm install -D typescript @types/node

2. Set Up TypeScript Configuration

2.1 Create tsconfig.json

Create a TypeScript configuration file to properly compile your code:

{
  "compilerOptions": {
    "target": "ES2020",
    "module": "commonjs",
    "lib": ["ES2020"],
    "types": ["node"],
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "outDir": "dist",
    "rootDir": "src",
    "strict": true
  },
  "include": ["src/**/*"]
}

3. Configure Platform Credentials

3.1 Create environment variables

Create a .env file in your project root to store your platform credentials:

VERCEL_TOKEN=your_vercel_token
CLOUDFLARE_TOKEN=your_cloudflare_token
RENDER_API_KEY=your_render_api_key
NETLIFY_TOKEN=your_netlify_token
RAILWAY_TOKEN=your_railway_token

3.2 Load environment variables

Install dotenv for environment variable handling:

npm install dotenv

4. Implement Domain Management Functions

4.1 Create the main domain manager

Create src/domainManager.ts with the following code:

import { DomainSDK } from '@opencoredev/domain-sdk';
import dotenv from 'dotenv';

// Load environment variables
dotenv.config();

// Initialize the SDK with your credentials
const sdk = new DomainSDK({
  vercel: process.env.VERCEL_TOKEN,
  cloudflare: process.env.CLOUDFLARE_TOKEN,
  render: process.env.RENDER_API_KEY,
  netlify: process.env.NETLIFY_TOKEN,
  railway: process.env.RAILWAY_TOKEN
});

export class DomainManager {
  static async addDomain(domain: string, platform: string) {
    try {
      console.log(`Adding domain ${domain} to ${platform}...`);
      const result = await sdk.addDomain(domain, platform);
      console.log('Domain added successfully:', result);
      return result;
    } catch (error) {
      console.error('Error adding domain:', error);
      throw error;
    }
  }

  static async verifyDomain(domain: string, platform: string) {
    try {
      console.log(`Verifying domain ${domain} on ${platform}...`);
      const result = await sdk.verifyDomain(domain, platform);
      console.log('Domain verification status:', result);
      return result;
    } catch (error) {
      console.error('Error verifying domain:', error);
      throw error;
    }
  }

  static async removeDomain(domain: string, platform: string) {
    try {
      console.log(`Removing domain ${domain} from ${platform}...`);
      const result = await sdk.removeDomain(domain, platform);
      console.log('Domain removed successfully:', result);
      return result;
    } catch (error) {
      console.error('Error removing domain:', error);
      throw error;
    }
  }
}

4.2 Create a test script

Create src/testDomains.ts to test your implementation:

import { DomainManager } from './domainManager';

async function testDomainManagement() {
  const testDomain = 'example.com';
  const platform = 'vercel'; // Change this to test different platforms

  try {
    // Add domain
    await DomainManager.addDomain(testDomain, platform);
    
    // Wait a bit for domain to be processed
    await new Promise(resolve => setTimeout(resolve, 5000));
    
    // Verify domain
    await DomainManager.verifyDomain(testDomain, platform);
    
    // Remove domain
    await DomainManager.removeDomain(testDomain, platform);
    
    console.log('All domain operations completed successfully!');
  } catch (error) {
    console.error('Test failed:', error);
  }
}

// Run the test
if (require.main === module) {
  testDomainManagement();
}

5. Run Your Domain Manager

5.1 Compile TypeScript code

Compile your TypeScript files to JavaScript:

npx tsc

5.2 Execute the test script

Run your domain management test:

node dist/testDomains.js

6. Advanced Usage Examples

6.1 Batch domain operations

Extend your domain manager with batch operations:

export class DomainManager {
  // ... previous methods ...

  static async batchAddDomains(domains: string[], platform: string) {
    const results = [];
    for (const domain of domains) {
      try {
        const result = await this.addDomain(domain, platform);
        results.push({ domain, success: true, result });
      } catch (error) {
        results.push({ domain, success: false, error: error.message });
      }
    }
    return results;
  }

  static async getDomainStatus(domain: string, platform: string) {
    try {
      const status = await sdk.getDomainStatus(domain, platform);
      console.log(`Status for ${domain}:`, status);
      return status;
    } catch (error) {
      console.error('Error getting domain status:', error);
      throw error;
    }
  }
}

6.2 Handle domain status monitoring

Implement status monitoring for better domain management:

async function monitorDomainStatus(domain: string, platform: string, maxAttempts = 10) {
  for (let i = 0; i < maxAttempts; i++) {
    try {
      const status = await DomainManager.getDomainStatus(domain, platform);
      console.log(`Attempt ${i + 1}:`, status);
      
      if (status.verified === true) {
        console.log('Domain is verified!');
        break;
      }
      
      // Wait before next check
      await new Promise(resolve => setTimeout(resolve, 3000));
    } catch (error) {
      console.error('Error checking status:', error);
      break;
    }
  }
}

Summary

In this tutorial, you've learned how to implement a comprehensive domain management system using OpenCoreDev's Domain SDK 0.2.0. You've set up a TypeScript project, configured platform credentials, and created functions to add, verify, and remove domains across multiple hosting platforms. The SDK abstracts the platform-specific differences, allowing you to manage domains with a unified API interface.

The key advantages of using this SDK include:

  • Single API interface for five different platforms
  • Consistent domain status modeling
  • Separate verification and certificate handling
  • Streamlined domain lifecycle management

This implementation provides a solid foundation for building more complex domain management applications, such as automated deployment pipelines or customer domain management dashboards.

Source: MarkTechPost

Related Articles