How it works

1

Create Connection

Create a connection using POST /connections by providing a connectorId and an ownerId. The ownerId is a unique identifier defined by you, typically representing a customer or user ID.

import { Morph } from "@runmorph/cloud";

// initialize the Morph client with your API credentials
const morph = Morph({
  publicKey: "pk_demo_xxxxxxxxxxxxxxx",
  secretKey: "sk_demo_xxxxxxxxxxxxxxx",
});

// initialize the connection
const connection = morph.connections({
  connectorId: "hubspot",
  ownerId: "demo",
});

// create the connection with the required operations
await connection.create({
  operations: [ "genericContact::list" ],
});
2

Authorize Connection

Authorize the connection using POST /authorization to obtain the authorizationUrl required to initiate the OAuth flow.

// auhtorize the connection
const { auhtorizationUrl } = await connection.authorize();

// redirect the user to the auhtorizationUrl
3

CRUD Resources

Once connected, you can perform CRUD operations on any unified resources available in the /resources endpoint. You can also make authenticated HTTP requests to the connector’s API using /proxy.

// CRUD Resources
// Access a specific resource model
const contacts = connection.resources("genericContact");

// Create a resource
const { data, error } = await contacts.create({
  firstName: "John",
  lastName: "Doe",
  email: "john.doe@corp.co"
});

// Retrieve a specific resource
const { data, error } = await contacts.retrieve("contact-123", {
  fields: ["email", "firstName"],
});

// List resources
const { data, next, error } = await contacts.list({
  limit: 10,
  cursor: "next-page-cursor",
});

// Update a resource
const { data, error } = await contacts.update("contact-123", {
  firstName: "Jane",
});

// Proxy
// Check the connector used for a given connection
const hubspotConnection = connection.isConnector("hubspot");
if (!hubspotConnection) return;

// Now you know for sure it's a HubSpot connection – you can call HubSpot API endpoint directly
const { data, error } = await hubspotConnection.proxy({
  path: "/v3/objects/contacts",
  method: "GET",
  query: { limit: 10 },
});

Let’s get started: