Skip to main content
Creates a new collection with the specified name and schema. Collections are containers for documents that share a common structure.

Method Signature

collections().create(
  name: string,
  schema: Record<string, FieldSpec>
): Promise<Collection>

Parameters

name
string
required
The name of the collection to create. Must be unique within the project.
schema
Record<string, FieldSpec>
required
The schema definition for the collection. Maps field names to their specifications, including data types and indexes.

Returns

Collection
object
Returns a Collection object containing metadata about the newly created collection.
name
string
Name of the collection
orgId
string
Organization ID that owns the collection
projectId
string
Project ID that contains the collection
schema
Record<string, CollectionFieldSpec>
Schema definition for the collection fields
region
string
Region where the collection is stored

Examples

Basic Text Collection

import { Client } from "topk-js";
import { text, int } from "topk-js/schema";

const client = new Client({
  apiKey: process.env.TOPK_API_KEY,
  region: "us-east-1"
});

const collection = await client.collections().create("books", {
  title: text().required(),
  author: text(),
  published_year: int()
});

console.log(`Created collection: ${collection.name}`);

Vector Search Collection

import { Client } from "topk-js";
import { text, f32Vector, vectorIndex } from "topk-js/schema";

const client = new Client({
  apiKey: process.env.TOPK_API_KEY,
  region: "us-east-1"
});

const collection = await client.collections().create("products", {
  name: text().required(),
  description: text(),
  embedding: f32Vector({ dimension: 1536 }).index(
    vectorIndex({ metric: "cosine" })
  )
});

console.log(`Created collection with vector index: ${collection.name}`);

Keyword Search Collection

import { Client } from "topk-js";
import { text, int, keywordIndex } from "topk-js/schema";

const client = new Client({
  apiKey: process.env.TOPK_API_KEY,
  region: "us-east-1"
});

const collection = await client.collections().create("articles", {
  title: text().required().index(keywordIndex()),
  content: text().index(keywordIndex()),
  views: int()
});

console.log(`Created collection with keyword indexes: ${collection.name}`);

Semantic Search Collection

import { Client } from "topk-js";
import { text, semanticIndex } from "topk-js/schema";

const client = new Client({
  apiKey: process.env.TOPK_API_KEY,
  region: "us-east-1"
});

const collection = await client.collections().create("documents", {
  title: text().required(),
  content: text().index(
    semanticIndex({ model: "cohere/embed-v4" })
  )
});

console.log(`Created collection with semantic index: ${collection.name}`);
Once created, a collection’s schema cannot be modified. Plan your schema carefully before creating the collection.

Build docs developers (and LLMs) love