Skip to main content

Authentication

The MCMP JavaScript SDK supports multiple authentication methods to secure your API requests.

API Key Authentication

The most common authentication method is using an API key:
import { McmpClient } from '@mcmp/sdk';

const client = new McmpClient({
  apiKey: 'your-api-key-here'
});

Getting an API Key

  1. Sign up for an MCMP account at mcmp.app
  2. Navigate to your dashboard
  3. Go to SettingsAPI Keys
  4. Click Generate New Key
  5. Copy the key and store it securely
Never commit API keys to version control. Use environment variables or secure configuration management.

Environment Variables

Store your API key in environment variables:
# .env
MCMP_API_KEY=your-api-key-here
Then use it in your code:
import { McmpClient } from '@mcmp/sdk';

const client = new McmpClient({
  apiKey: process.env.MCMP_API_KEY
});

Configuration Objects

Create a configuration object for your API key:
// config.js
export const mcmpConfig = {
  apiKey: process.env.MCMP_API_KEY,
  baseUrl: 'https://api.mcmp.app/v1',
  timeout: 30000,
  retries: 3
};

// Usage
import { McmpClient } from '@mcmp/sdk';
import { mcmpConfig } from './config.js';

const client = new McmpClient(mcmpConfig);

OAuth 2.0 Authentication

For applications that need user-specific access, use OAuth 2.0:
import { McmpClient, OAuth2Credentials } from '@mcmp/sdk';

const credentials = new OAuth2Credentials({
  clientId: 'your-client-id',
  clientSecret: 'your-client-secret',
  refreshToken: 'your-refresh-token'
});

const client = new McmpClient({
  credentials: credentials
});

OAuth 2.0 Flow

1

Register Application

Register your application in the MCMP dashboard to get client credentials
2

Authorization URL

Redirect users to the MCMP authorization URL with your client ID
3

Exchange Code

Exchange the authorization code for access and refresh tokens
4

Use Tokens

Use the access token for API requests and refresh when needed

JWT Token Authentication

For server-to-server communication, use JWT tokens:
import { McmpClient, JWTCredentials } from '@mcmp/sdk';

const credentials = new JWTCredentials({
  token: 'your-jwt-token'
});

const client = new McmpClient({
  credentials: credentials
});

Custom Authentication

Implement custom authentication by extending the Credentials class:
import { McmpClient, Credentials } from '@mcmp/sdk';

class CustomCredentials extends Credentials {
  constructor(customToken) {
    super();
    this.customToken = customToken;
  }
  
  apply(context) {
    context.headers['X-Custom-Auth'] = this.customToken;
  }
}

// Usage
const client = new McmpClient({
  credentials: new CustomCredentials('your-custom-token')
});

Browser Authentication

For browser applications, use secure storage:
// Store API key securely
localStorage.setItem('mcmp-api-key', 'your-api-key');

// Retrieve and use
const client = new McmpClient({
  apiKey: localStorage.getItem('mcmp-api-key')
});

React Authentication

For React applications, use context for authentication:
// AuthContext.js
import React, { createContext, useContext } from 'react';
import { McmpClient } from '@mcmp/sdk';

const AuthContext = createContext();

export function AuthProvider({ children }) {
  const client = new McmpClient({
    apiKey: process.env.REACT_APP_MCMP_API_KEY
  });
  
  return (
    <AuthContext.Provider value={client}>
      {children}
    </AuthContext.Provider>
  );
}

export function useMcmpClient() {
  return useContext(AuthContext);
}

Vue.js Authentication

For Vue.js applications, use composables:
// useMcmp.js
import { ref, computed } from 'vue';
import { McmpClient } from '@mcmp/sdk';

const client = ref(null);

export function useMcmp() {
  const initializeClient = (apiKey) => {
    client.value = new McmpClient({ apiKey });
  };
  
  const isAuthenticated = computed(() => client.value !== null);
  
  return {
    client,
    initializeClient,
    isAuthenticated
  };
}

Security Best Practices

Environment Variables

Store sensitive credentials in environment variables, not in code

Key Rotation

Regularly rotate your API keys for enhanced security

Scope Limitation

Use the minimum required permissions for your API keys

Secure Storage

Use secure credential storage solutions in production

Error Handling

Handle authentication errors gracefully:
import { McmpClient, AuthenticationError, AuthorizationError } from '@mcmp/sdk';

try {
  const client = new McmpClient({ apiKey: 'invalid-key' });
  const servers = await client.servers.list();
} catch (error) {
  if (error instanceof AuthenticationError) {
    console.error('Authentication failed:', error.message);
    // Handle invalid credentials
  } else if (error instanceof AuthorizationError) {
    console.error('Authorization failed:', error.message);
    // Handle insufficient permissions
  } else {
    console.error('Unexpected error:', error.message);
  }
}

Testing Authentication

Test your authentication setup:
async function testAuthentication() {
  try {
    const client = new McmpClient({
      apiKey: process.env.MCMP_API_KEY
    });
    
    // Test authentication by making a simple API call
    await client.servers.list();
    console.log('Authentication successful!');
    
  } catch (error) {
    console.error('Authentication failed:', error.message);
  }
}

testAuthentication();

Next Steps

1

Server Management

Learn to manage servers with the SDK
2

Error Handling

Handle errors and exceptions properly