@runmorph/cloud
A powerful TypeScript SDK for seamlessly integrating Morph's unified API into your backend services and fronted apps. Built with TypeScript for type safety and modern development practices.
Features
- 🔐 Built-in authentication handling (only OAuth2 for now)
- 🔌 Unified connector interface for any platforms
- 📦 Modular architecture with pluggable connectors
- 🔄 Standardized CRUD operations for resources
- 🌐 Proxy capabilities for direct API access
- 📝 Type-safe operations with full TypeScript support
Installation
yarn add @runmorph/cloud
Quick Start
1. Initialize the Client
import { Morph } from "@runmorph/cloud";
const morph = new Morph({
publicKey: process.env.MORPH_PUBLIC_KEY!,
secretKey: process.env.MORPH_SECRET_KEY!,
});
# .env.local
MORPH_PUBLIC_KEY=pk_demo_xxxxxxxxxxxxxxx
MORPH_API_SECRET=sk_demo_xxxxxxxxxxxxxxx
Note: You can use these demo credentials to quickly test the SDK. For production use, replace them with your own API keys from the Morph dashboard.
2. Create and Authorize Connections
Use the <Connect />
React component from @runmorph/atoms
(see documentation) or build your own custom authorization flow:
const connection = morph.connections({
connectorId: "hubspot",
ownerId: "demo",
});
const { error } = await connection.create({
operations: ["genericContact::list"],
});
const { authorizationUrl } = await connection.authorize();
3. Work with Resources
const contacts = connection.resources("genericContact");
const { data: newContact } = await contacts.create({
firstName: "John",
lastName: "Doe",
email: "john.doe@example.com",
});
const { data: contactsList, next } = await contacts.list({
limit: 10,
cursor: "next-page-cursor",
});
const { data: contact } = await contacts.retrieve("contact-123", {
fields: ["email", "firstName", "lastName"],
});
const { data: updatedContact } = await contacts.update("contact-123", {
firstName: "Jane",
});
Advanced Usage
Type-Safe Connector Access
const hubspotConnection = connection.isConnector("hubspot");
if (!hubspotConnection) return;
const { data } = await hubspotConnection.proxy({
path: "/v3/objects/contacts",
method: "GET",
query: { limit: 10 },
});
Session Token Generation
Create a session token to interact with the Morph API from your client-facing app
const { sessionToken } = await morph.sessions().create({
connection: {
connectorId: "hubspot",
ownerId: "demo",
},
expiresIn: 30 * 60 * 60,
});
Error Handling
try {
const { data, error } = await contacts.create({
firstName: "John",
email: "invalid-email",
});
if (error) {
console.error("Operation failed:", error.message);
return;
}
console.log("Contact created:", data);
} catch (e) {
console.error("Unexpected error:", e);
}
Resource Operations
The SDK provides consistent CRUD operations across all supported connectors:
create
: Create new resourcesretrieve
: Fetch specific resourceslist
: List resources with paginationupdate
: Update existing resourcesdelete
: Remove resources
Each operation returns a typed response with the following structure:
interface Response<T> {
data?: T;
error?: {
message: string;
code: string;
};
next?: string;
}
TypeScript Support
The SDK is written in TypeScript and provides comprehensive type definitions. Enable strict mode in your tsconfig.json
for the best development experience:
{
"compilerOptions": {
"strict": true
}
}
Security Best Practices
- Never expose your secret key in client-side code
- Store API credentials securely using environment variables
- Generate session tokens server-side only
- Implement proper error handling
- Validate all input data before making API calls
Frontend Integration
For frontend integration, we recommend using @runmorph/atoms, our React SDK that provides pre-built components and hooks for a seamless user experience.
API Reference
For detailed API documentation, visit our official documentation.