Getting Started

The Caisy TypeScript SDK exposes the Caisy GraphQL schema through strongly typed models and operations. It's written in TypeScript but can also be used in any JavaScript environment.

All operations return models, which can be used to perform operations for other models, and all types are accessible through the Caisy SDK package.

import { initSdk } from "@caisy/sdk";

const sdk = initSdk({
    token: token,
});

const getManyBlueprintsResult = await sdk.GetManyBlueprints({ input: { projectId: "aa0943f2-af8e-4920-afe5-96e7c6fb7b02" } });

Connect to the Caisy API and interact with your data in a few steps:

Install the Caisy SDK

Using npm:

npm install @caisy/sdk

Or yarn:

yarn add @caisy/sdk

Authentication

First, you need to set up the SDK with a token. The best way to do this is to create a permanent personal access token:

Use the token when you init the SDK like this:

const sdk = initSdk({
    token: "<your personal access token>",
}); 


In an IDE with Typescript autocomplete support, like in Visual Studio Code, you can see all the possible queries you can make directly with the SDK.

screenshot sdk code create

Query Data

Suppose you want to retrieve all the blueprints of your project. The code is as follows:

const getManyBlueprintsResult = await sdk.GetManyBlueprints({ input: { projectId: "aa0943f2-af8e-4920-afe5-96e7c6fb7b02" } });

Get a specific document by ID

If you want to retrieve a specific document by its ID, you can use the GetDocumentById query:

const getDocumentByIdResult = await sdk.GetDocumentById({
    input: {
        documentId: "ac3f8e92-7c1f-4d2a-9d4c-7b9a1f8d3e6f",
        projectId: "aa0943f2-af8e-4920-afe5-96e7c6fb7b02",
    },
});

Get many documents with filtering and sorting

To retrieve multiple documents with filtering and sorting options, you can use the GetManyDocuments query:

const getManyDocumentsResult = await sdk.GetManyDocuments({
    input: {
        projectId: "aa0943f2-af8e-4920-afe5-96e7c6fb7b02",
        metaFilter: {
            blueprint: {
                blueprintId: "41fcf4c7-b04b-4715-80fb-ebccfa70ac11",
                compare: "PURE_IS",
            },
        },
        sort: [
            {
                field: {
                    id: "name",
                    type: "BLUEPRINT_FIELD_TYPE_STRING",
                },
                direction: "DIRECTION_ASC",
            },
        ],
    },
});

Get a specific blueprint by name

To retrieve a specific blueprint by its name, you can use the GetBlueprintByName query:

const getBlueprintByNameResult = await sdk.GetBlueprintByName({
    input: {
        blueprintName: "Product",
        projectId: "aa0943f2-af8e-4920-afe5-96e7c6fb7b02",
    },
});

Modify Data

Create a new document

To create a new document, you can use the CreateDocument mutation:

const createDocumentResult = await sdk.CreateDocument({
    input: {
        blueprintId: "41fcf4c7-b04b-4715-80fb-ebccfa70ac11",
        projectId: "aa0943f2-af8e-4920-afe5-96e7c6fb7b02",
    },
});

Update an existing document

To update an existing document, you can use the UpdateDocument mutation:

const updateDocumentResult = await sdk.UpdateDocument({
    input: {
        documentId: "ac3f8e92-7c1f-4d2a-9d4c-7b9a1f8d3e6f",
        projectId: "aa0943f2-af8e-4920-afe5-96e7c6fb7b02",
        title: "Updated Document Title",
    },
});

Delete a document

To delete a document, you can use the DeleteDocument mutation:

const deleteDocumentResult = await sdk.DeleteDocument({
    input: {
        documentId: "ac3f8e92-7c1f-4d2a-9d4c-7b9a1f8d3e6f",
        projectId: "aa0943f2-af8e-4920-afe5-96e7c6fb7b02",
    },
});


Or you could duplicate a project using the SDK as follows:

await sdk.DuplicateToProject({
        input: {
            projectId: "b85f60bc-26c6-41cd-9a30-69f64a8ec3c3",
            source: {
                projectId: "41fcf4c7-b04b-4715-80fb-ebccfa70ac11",
            },
            selection: {
                blueprint: true,
            },
        },
    });


These examples demonstrate how you can use the Caisy TypeScript SDK to perform various queries and mutations on your data. The SDK provides a strongly typed and intuitive way to interact with the Caisy API, making it easier to work with your data in a TypeScript or JavaScript environment.

Remember to replace the placeholder IDs and values with your actual project and data IDs.