# Collections

## Overview

Collections provide a way to attach data to activities that is shared between these activities and can be updated in a single place.
A typical use case is to track the status of an activity such as `started`, `completed`, or `failed`. Instead of having to update multiple activities when the status changes you can create a collection and add it to the activities that needs this data. When the status changes you only need to update the collection.

A collection consists of a `name`, an `id` and some data. You can think of the `name` as a namespace for the collection. A **collection reference** is a string that looks like `<name>:<id>` and is the unique identifier for the collection.

<admonition type="info">

Data stored in collections can not be used in custom ranking or aggregation.

</admonition>

<admonition type="info">

The batch endpoints supports up to 100 collections in a single request.
Automatic deduplication will be applied, keeping the last occurrence only.

</admonition>

## Creating Collections

The example below shows how to create a collection.

<Tabs>

```swift label="Swift"
let response = try await client.createCollections(
    request: .init(
        collections: [
            .init(
                name: "movies",
                id: "lord_of_the_rings",
                custom: [
                    "title": "Lord of the Rings",
                    "genre": "fantasy",
                    "rating": 9
                ]
            )
        ]
    )
)
```

```kotlin label="Kotlin"
val response: Result<CreateCollectionsResponse> = client.createCollections(
    request = CreateCollectionsRequest(
        collections = listOf(
            CollectionRequest(
                name = "movies",
                id = "lord_of_the_rings",
                custom = mapOf(
                    "title" to "Lord of the Rings",
                    "genre" to "fantasy",
                    "rating" to 9
                )
            )
        )
    )
)
```

```js label="JavaScript"
const response = await client.createCollections({
  collections: [
    {
      name: "movies",
      id: "lord_of_the_rings",
      custom: {
        title: "Lord of the Rings",
        genre: "fantasy",
        rating: 9,
      },
    },
  ],
});
```

```js label="React"
const response = await client.createCollections({
  collections: [
    {
      name: "movies",
      id: "lord_of_the_rings",
      custom: {
        title: "Lord of the Rings",
        genre: "fantasy",
        rating: 9,
      },
    },
  ],
});
```

```js label="React Native"
const response = await client.createCollections({
  collections: [
    {
      name: "movies",
      id: "lord_of_the_rings",
      custom: {
        title: "Lord of the Rings",
        genre: "fantasy",
        rating: 9,
      },
    },
  ],
});
```

```js label="Node"
const response = await client.feeds.createCollections({
  collections: [
    {
      name: "movies",
      id: "lord_of_the_rings",
      custom: {
        title: "Lord of the Rings",
        genre: "fantasy",
        rating: 9,
      },
    },
  ],
});
```

```dart label="Dart"
final response = await client.createCollections(
  collections: [
    const CollectionRequest(
      name: 'movies',
      id: 'lord_of_the_rings',
      custom: {
        'title': 'Lord of the Rings',
        'genre': 'fantasy',
        'rating': 9,
      },
    ),
  ],
);
```

```go label="Go"
response, err := client.Feeds().CreateCollections(context.Background(), &getstream.CreateCollectionsRequest{
	Collections: []getstream.CollectionRequest{
		{
			Name: "movies",
			ID:   "lord_of_the_rings",
			Custom: map[string]any{
				"title":  "Lord of the Rings",
				"genre":  "fantasy",
				"rating": 9,
			},
		},
	},
})
if err != nil {
	log.Fatal("Error creating collections:", err)
}
```

```java label="Java"
Map<String, Object> customData = new HashMap<>();
customData.put("title", "Lord of the Rings");
customData.put("genre", "fantasy");
customData.put("rating", 9);

CollectionRequest collection = CollectionRequest.builder()
    .name("movies")
    .id("lord_of_the_rings")
    .custom(customData)
    .build();

CreateCollectionsRequest request = CreateCollectionsRequest.builder()
    .collections(List.of(collection))
    .build();

CreateCollectionsResponse response = feeds.createCollections(request).execute().getData();
```

```php label="php"
$collection = new GeneratedModels\CollectionRequest(
    name: 'movies',
    id: 'lord_of_the_rings',
    custom: (object)[
        'title' => 'Lord of the Rings',
        'genre' => 'fantasy',
        'rating' => 9,
    ]
);

$response = $feedsClient->createCollections(
    new GeneratedModels\CreateCollectionsRequest(collections: [$collection])
);
```

```csharp label="C#"
var collection = new CollectionRequest
{
    Name = "movies",
    ID = "lord_of_the_rings",
    Custom = new Dictionary<string, object>
    {
        { "title", "Lord of the Rings" },
        { "genre", "fantasy" },
        { "rating", 9 }
    }
};

var response = await _feedsV3Client.CreateCollectionsAsync(
    new CreateCollectionsRequest { Collections = new List<CollectionRequest> { collection } }
);
```

```python label="Python"
response = client.feeds.create_collections(
    collections=[
        CollectionRequest(
            name="movies",
            id="lord_of_the_rings",
            custom={
                "title": "Lord of the Rings",
                "genre": "fantasy",
                "rating": 9,
            }
        )
    ]
)
```

```ruby label="Ruby"
collection = GetStream::Generated::Models::CollectionRequest.new(
  name: 'movies',
  id: 'lord_of_the_rings',
  custom: {
    title: 'Lord of the Rings',
    genre: 'fantasy',
    rating: 9
  }
)

request = GetStream::Generated::Models::CreateCollectionsRequest.new(
  collections: [collection]
)

response = client.feeds.create_collections(request)
```

</Tabs>

### Overview of the collection model

<open-api-models modelname="CollectionResponse" recursive="false" headerlevel="4">
</open-api-models>

## Updating Collections

The example below shows how to update a collection.

<Tabs>

```swift label="Swift"
let response = try await client.updateCollections(
    request: .init(
        collections: [
            .init(
                name: "movies",
                id: "lord_of_the_rings",
                custom: [
                    "title": "Lord of the Rings",
                    "genre": "fantasy",
                    "rating": 9.5
                ]
            )
        ]
    )
)
```

```kotlin label="Kotlin"
val response: Result<UpdateCollectionsResponse> = client.updateCollections(
    request = UpdateCollectionsRequest(
        collections = listOf(
            UpdateCollectionRequest(
                name = "movies",
                id = "lord_of_the_rings",
                custom = mapOf(
                    "title" to "Lord of the Rings",
                    "genre" to "fantasy",
                    "rating" to 9.5
                )
            )
        )
    )
)
```

```js label="JavaScript"
const response = await client.updateCollections({
  collections: [
    {
      name: "movies",
      id: "lord_of_the_rings",
      custom: {
        title: "Lord of the Rings",
        genre: "fantasy",
        rating: 9.5,
      },
    },
  ],
});
```

```js label="React"
const response = await client.updateCollections({
  collections: [
    {
      name: "movies",
      id: "lord_of_the_rings",
      custom: {
        title: "Lord of the Rings",
        genre: "fantasy",
        rating: 9.5,
      },
    },
  ],
});
```

```js label="React Native"
const response = await client.updateCollections({
  collections: [
    {
      name: "movies",
      id: "lord_of_the_rings",
      custom: {
        title: "Lord of the Rings",
        genre: "fantasy",
        rating: 9.5,
      },
    },
  ],
});
```

```js label="Node"
const response = await client.feeds.updateCollections({
  collections: [
    {
      name: "movies",
      id: "lord_of_the_rings",
      custom: {
        title: "Lord of the Rings",
        genre: "fantasy",
        rating: 9.5,
      },
    },
  ],
});
```

```dart label="Dart"
final response = await client.updateCollections(
  collections: [
    const CollectionRequest(
      name: 'movies',
      id: 'lord_of_the_rings',
      custom: {
        'title': 'Lord of the Rings',
        'genre': 'fantasy',
        'rating': 9.5,
      },
    ),
  ],
);
```

```go label="Go"
response, err := client.Feeds().UpdateCollections(context.Background(), &getstream.UpdateCollectionsRequest{
	Collections: []getstream.CollectionRequest{
		{
			Name: "movies",
			ID:   "lord_of_the_rings",
			Custom: map[string]any{
				"title":  "Lord of the Rings",
				"genre":  "fantasy",
				"rating": 9.5,
			},
		},
	},
})
if err != nil {
	log.Fatal("Error updating collections:", err)
}
```

```java label="Java"
Map<String, Object> customData = new HashMap<>();
customData.put("title", "Lord of the Rings");
customData.put("genre", "fantasy");
customData.put("rating", 9.5);

CollectionRequest collection = CollectionRequest.builder()
    .name("movies")
    .id("lord_of_the_rings")
    .custom(customData)
    .build();

UpdateCollectionsRequest request = UpdateCollectionsRequest.builder()
    .collections(List.of(collection))
    .build();

UpdateCollectionsResponse response = feeds.updateCollections(request).execute().getData();
```

```php label="php"
$collection = new GeneratedModels\CollectionRequest(
    name: 'movies',
    id: 'lord_of_the_rings',
    custom: (object)[
        'title' => 'Lord of the Rings',
        'genre' => 'fantasy',
        'rating' => 9.5,
    ]
);

$response = $feedsClient->updateCollections(
    new GeneratedModels\UpdateCollectionsRequest(collections: [$collection])
);
```

```csharp label="C#"
var collection = new CollectionRequest
{
    Name = "movies",
    ID = "lord_of_the_rings",
    Custom = new Dictionary<string, object>
    {
        { "title", "Lord of the Rings" },
        { "genre", "fantasy" },
        { "rating", 9.5 }
    }
};

var response = await _feedsV3Client.UpdateCollectionsAsync(
    new UpdateCollectionsRequest { Collections = new List<CollectionRequest> { collection } }
);
```

```python label="Python"
response = client.feeds.update_collections(
    collections=[
        CollectionRequest(
            name="movies",
            id="lord_of_the_rings",
            custom={
                "title": "Lord of the Rings",
                "genre": "fantasy",
                "rating": 9.5,
            }
        )
    ]
)
```

```ruby label="Ruby"
collection = GetStream::Generated::Models::CollectionRequest.new(
  name: 'movies',
  id: 'lord_of_the_rings',
  custom: {
    title: 'Lord of the Rings',
    genre: 'fantasy',
    rating: 9.5
  }
)

request = GetStream::Generated::Models::UpdateCollectionsRequest.new(
  collections: [collection]
)

response = client.feeds.update_collections(request)
```

</Tabs>

## Upserting Collections

The upsert endpoint creates a collection if it does not exist, or updates it if it already exists. This is useful when you want to ensure a collection is in a specific state without checking for its existence first.

<admonition type="info">

The upsert collections endpoint is server-side only.

</admonition>

<Tabs>

```js label="Node"
serverClient.feeds.upsertCollections({
  collections: [
    {
      name: "movies",
      id: "lord_of_the_rings",
      custom: {
        title: "Lord of the Rings",
        genre: "fantasy",
        rating: 9.5,
      },
    },
  ],
});
```

```go label="Go"
response, err := client.Feeds().UpsertCollections(context.Background(), &getstream.UpsertCollectionsRequest{
	Collections: []getstream.CollectionRequest{
		{
			Name: "movies",
			ID:   "lord_of_the_rings",
			Custom: map[string]any{
				"title":  "Lord of the Rings",
				"genre":  "fantasy",
				"rating": 9.5,
			},
		},
	},
})
if err != nil {
	log.Fatal("Error upserting collections:", err)
}
```

```java label="Java"
Map<String, Object> customData = new HashMap<>();
customData.put("title", "Lord of the Rings");
customData.put("genre", "fantasy");
customData.put("rating", 9.5);

CollectionRequest collection = CollectionRequest.builder()
    .name("movies")
    .id("lord_of_the_rings")
    .custom(customData)
    .build();

UpsertCollectionsRequest request = UpsertCollectionsRequest.builder()
    .collections(List.of(collection))
    .build();

UpsertCollectionsResponse response = feeds.upsertCollections(request).execute().getData();
```

```php label="php"
$collection = new GeneratedModels\CollectionRequest(
    name: 'movies',
    id: 'lord_of_the_rings',
    custom: (object)[
        'title' => 'Lord of the Rings',
        'genre' => 'fantasy',
        'rating' => 9.5,
    ]
);

$response = $feedsClient->upsertCollections(
    new GeneratedModels\UpsertCollectionsRequest(collections: [$collection])
);
```

```csharp label="C#"
var collection = new CollectionRequest
{
    Name = "movies",
    ID = "lord_of_the_rings",
    Custom = new Dictionary<string, object>
    {
        { "title", "Lord of the Rings" },
        { "genre", "fantasy" },
        { "rating", 9.5 }
    }
};

var response = await _feedsV3Client.UpsertCollectionsAsync(
    new UpsertCollectionsRequest { Collections = new List<CollectionRequest> { collection } }
);
```

```python label="Python"
response = client.feeds.upsert_collections(
    collections=[
        CollectionRequest(
            name="movies",
            id="lord_of_the_rings",
            custom={
                "title": "Lord of the Rings",
                "genre": "fantasy",
                "rating": 9.5,
            }
        )
    ]
)
```

```ruby label="Ruby"
collection = GetStream::Generated::Models::CollectionRequest.new(
  name: 'movies',
  id: 'lord_of_the_rings',
  custom: {
    title: 'Lord of the Rings',
    genre: 'fantasy',
    rating: 9.5
  }
)

request = GetStream::Generated::Models::UpsertCollectionsRequest.new(
  collections: [collection]
)

response = client.feeds.upsert_collections(request)
```

</Tabs>

## Deleting Collections

The example below shows how to delete a collection.

<Tabs>

```swift label="Swift"
let response = try await client.deleteCollections(
    request: .init(
        collectionRefs: ["movies:lord_of_the_rings"]
    )
)
```

```kotlin label="Kotlin"
val response: Result<DeleteCollectionsResponse> = client.deleteCollections(
    refs = listOf("movies:lord_of_the_rings")
)
```

```js label="JavaScript"
const response = await client.deleteCollections({
  collection_refs: ["movies:lord_of_the_rings"],
});
```

```js label="React"
const response = await client.deleteCollections({
  collection_refs: ["movies:lord_of_the_rings"],
});
```

```js label="React Native"
const response = await client.deleteCollections({
  collection_refs: ["movies:lord_of_the_rings"],
});
```

```js label="Node"
const response = await client.feeds.deleteCollections({
  collection_refs: ["movies:lord_of_the_rings"],
});
```

```dart label="Dart"
final response = await client.deleteCollections(
  collectionRefs: ['movies:lord_of_the_rings'],
);
```

```go label="Go"
response, err := client.Feeds().DeleteCollections(context.Background(), &getstream.DeleteCollectionsRequest{
	CollectionRefs: []string{"movies:lord_of_the_rings"},
})
if err != nil {
	log.Fatal("Error deleting collections:", err)
}
```

```java label="Java"
DeleteCollectionsRequest request = DeleteCollectionsRequest.builder()
    .collectionRefs(List.of("movies:lord_of_the_rings"))
    .build();

DeleteCollectionsResponse response = feeds.deleteCollections(request).execute().getData();
```

```php label="php"
$response = $feedsClient->deleteCollections(
    new GeneratedModels\DeleteCollectionsRequest(
        collectionRefs: ['movies:lord_of_the_rings']
    )
);
```

```csharp label="C#"
var response = await _feedsV3Client.DeleteCollectionsAsync(
    new DeleteCollectionsRequest { CollectionRefs = new List<string> { "movies:lord_of_the_rings" } }
);
```

```python label="Python"
response = client.feeds.delete_collections(
    collection_refs=["movies:lord_of_the_rings"]
)
```

```ruby label="Ruby"
request = GetStream::Generated::Models::DeleteCollectionsRequest.new(
  collection_refs: ['movies:lord_of_the_rings']
)

response = client.feeds.delete_collections(request)
```

</Tabs>

## Reading Collections

The example below shows how to read a collection.

<Tabs>

```swift label="Swift"
let response = try await client.readCollections(
    request: .init(
        collectionRefs: ["movies:lord_of_the_rings"]
    )
)
```

```kotlin label="Kotlin"
val response: Result<ReadCollectionsResponse> = client.readCollections(
    refs = listOf("movies:lord_of_the_rings")
)
```

```js label="JavaScript"
const response = await client.readCollections({
  collection_refs: ["movies:lord_of_the_rings"],
});
```

```js label="React"
const response = await client.readCollections({
  collection_refs: ["movies:lord_of_the_rings"],
});
```

```js label="React Native"
const response = await client.readCollections({
  collection_refs: ["movies:lord_of_the_rings"],
});
```

```js label="Node"
const response = await client.feeds.readCollections({
  collection_refs: ["movies:lord_of_the_rings"],
});
```

```dart label="Dart"
final response = await client.readCollections(
  collectionRefs: ['movies:lord_of_the_rings'],
);
```

```go label="Go"
response, err := client.Feeds().ReadCollections(context.Background(), &getstream.ReadCollectionsRequest{
	CollectionRefs: []string{"movies:lord_of_the_rings"},
})
if err != nil {
	log.Fatal("Error reading collections:", err)
}
```

```java label="Java"
ReadCollectionsRequest request = ReadCollectionsRequest.builder()
    .collectionRefs(List.of("movies:lord_of_the_rings"))
    .build();

ReadCollectionsResponse response = feeds.readCollections(request).execute().getData();
```

```php label="php"
$response = $feedsClient->readCollections(
    new GeneratedModels\ReadCollectionsRequest(
        collectionRefs: ['movies:lord_of_the_rings']
    )
);
```

```csharp label="C#"
var response = await _feedsV3Client.ReadCollectionsAsync(
    new ReadCollectionsRequest { CollectionRefs = new List<string> { "movies:lord_of_the_rings" } }
);
```

```python label="Python"
response = client.feeds.read_collections(
    collection_refs=["movies:lord_of_the_rings"]
)
```

```ruby label="Ruby"
request = GetStream::Generated::Models::ReadCollectionsRequest.new(
  collection_refs: ['movies:lord_of_the_rings']
)

response = client.feeds.read_collections(request)
```

</Tabs>

## Activities and Enrichment

Collections are added to activities in the form of **collection references**. An activity can reference up to 10 collections.

<admonition type="info">

Support for adding collections directly when creating activities is coming soon.

</admonition>

### Adding Collections to an Activity

<Tabs>

```swift label="Swift"
let response = try await feed.addActivity(
    request: .init(
        type: "post",
        text: "I love this movie!",
        collectionRefs: ["movies:lord_of_the_rings"]
    )
)
```

```kotlin label="Kotlin"
val response: Result<ActivityData> = feed.addActivity(
    request = FeedAddActivityRequest(
        type = "post",
        text = "I love this movie!",
        collectionRefs = listOf("movies:lord_of_the_rings")
    )
)
```

```js label="JavaScript"
const response = await feed.addActivity({
  type: "post",
  text: "I love this movie!",
  collection_refs: ["movies:lord_of_the_rings"],
});
```

```js label="React"
const response = await feed.addActivity({
  type: "post",
  text: "I love this movie!",
  collection_refs: ["movies:lord_of_the_rings"],
});
```

```js label="React Native"
const response = await feed.addActivity({
  type: "post",
  text: "I love this movie!",
  collection_refs: ["movies:lord_of_the_rings"],
});
```

```js label="Node"
const response = await client.feeds.addActivity({
    type: 'post',
    feeds: [...],
    text: 'I love this movie!',
    collection_refs: ['movies:lord_of_the_rings'],
});
```

```dart label="Dart"
final response = await feed.addActivity(
  request: const FeedAddActivityRequest(
    type: 'post',
    text: 'I love this movie!',
    collectionRefs: ['movies:lord_of_the_rings'],
  ),
);
```

```go label="Go"
feedsClient := client.Feeds()
response, err := feedsClient.AddActivity(context.Background(), &getstream.AddActivityRequest{
	Type:          "post",
	Feeds:         []string{"user:jack"},
	Text:          getstream.PtrTo("I love this movie!"),
	UserID:        getstream.PtrTo("jack"),
	CollectionRefs: []string{"movies:lord_of_the_rings"},
})
if err != nil {
	log.Fatal("Error adding activity:", err)
}
```

```java label="Java"
AddActivityRequest activity =
    AddActivityRequest.builder()
        .type("post")
        .feeds(List.of("user:jack"))
        .text("I love this movie!")
        .userID("jack")
        .collectionRefs(List.of("movies:lord_of_the_rings"))
        .build();

AddActivityResponse response = feeds.addActivity(activity).execute().getData();
```

```php label="php"
$activity = new GeneratedModels\AddActivityRequest(
    type: 'post',
    feeds: ['user:jack'],
    text: 'I love this movie!',
    userID: 'jack',
    collectionRefs: ['movies:lord_of_the_rings']
);
$response = $feedsClient->addActivity($activity);
```

```csharp label="C#"
var activity = new AddActivityRequest
{
    Type = "post",
    Text = "I love this movie!",
    UserID = "jack",
    Feeds = new List<string> { "user:jack" },
    CollectionRefs = new List<string> { "movies:lord_of_the_rings" }
};
var response = await _feedsV3Client.AddActivityAsync(activity);
```

```python label="Python"
response = client.feeds.add_activity(
    type="post",
    feeds=["user:jack"],
    text="I love this movie!",
    user_id="jack",
    collection_refs=["movies:lord_of_the_rings"]
)
```

```ruby label="Ruby"
activity_request = GetStream::Generated::Models::AddActivityRequest.new(
  type: 'post',
  text: 'I love this movie!',
  user_id: 'jack',
  feeds: ['user:jack'],
  collection_refs: ['movies:lord_of_the_rings']
)

response = client.feeds.add_activity(activity_request)
```

</Tabs>

### Enrichment

When you have added collection references to your activities these will automatically be enriched with the collection data when reading feeds.

<Tabs>

```swift label="Swift"
// Create a collection
let createResponse = try await client.createCollections(
    request: .init(
        collections: [
            .init(
                name: "movies",
                id: "lord_of_the_rings",
                custom: [
                    "title": "Lord of the Rings",
                    "genre": "fantasy",
                    "rating": 9
                ]
            )
        ]
    )
)

// Add the reference to an activity
let feed = client.feed(group: "user", id: "jack")
let activityResponse = try await feed.addActivity(
    request: .init(
        type: "post",
        text: "I love this movie!",
        collectionRefs: ["movies:lord_of_the_rings"]
    )
)

// Read the feed and see the enriched collection data
try await feed.getOrCreate()
let activities = feed.state.activities
// The activities will contain enriched collection data in the collections field
// Example response structure:
// {
//     "activities": [
//         {
//             "type": "post",
//             "text": "I love this movie!",
//             "collections": {
//                 "movies:lord_of_the_rings": {
//                     "name": "movies",
//                     "id": "lord_of_the_rings",
//                     "custom": {
//                         "title": "Lord of the Rings",
//                         "genre": "fantasy",
//                         "rating": 9
//                     },
//                     "user_id": "jack",
//                     "created_at": "2025-01-01T00:00:00.000Z",
//                     "updated_at": "2025-01-01T00:00:00.000Z",
//                     "status": "ok"
//                 }
//             }
//         }
//     ],
//     ...
// }
```

```kotlin label="Kotlin"
// Create a collection
val createResponse: Result<CreateCollectionsResponse> = client.createCollections(
    request = CreateCollectionsRequest(
        collections = listOf(
            CollectionRequest(
                name = "movies",
                id = "lord_of_the_rings",
                custom = mapOf(
                    "title" to "Lord of the Rings",
                    "genre" to "fantasy",
                    "rating" to 9
                )
            )
        )
    )
)

// Add the reference to an activity
val feed = client.feed(group = "user", id = "jack")
val activityResponse: Result<ActivityData> = feed.addActivity(
    request = FeedAddActivityRequest(
        type = "post",
        text = "I love this movie!",
        collectionRefs = listOf("movies:lord_of_the_rings")
    )
)

// Read the feed and see the enriched collection data
feed.getOrCreate()
val activities = feed.state.activities
// The activities will contain enriched collection data in the collections field
// Example response structure:
// {
//     "activities": [
//         {
//             "type": "post",
//             "text": "I love this movie!",
//             "collections": {
//                 "movies:lord_of_the_rings": {
//                     "name": "movies",
//                     "id": "lord_of_the_rings",
//                     "custom": {
//                         "title": "Lord of the Rings",
//                         "genre": "fantasy",
//                         "rating": 9
//                     },
//                     "user_id": "jack",
//                     "created_at": "2025-01-01T00:00:00.000Z",
//                     "updated_at": "2025-01-01T00:00:00.000Z",
//                     "status": "ok"
//                 }
//             }
//         }
//     ],
//     ...
// }
```

```js label="JavaScript"
// Create a collection
await client.createCollections({
  collections: [
    {
      name: "movies",
      id: "lord_of_the_rings",
      custom: {
        title: "Lord of the Rings",
        genre: "fantasy",
        rating: 9,
      },
    },
  ],
});

// Add the reference to an activity
const feed = client.feed("user", "jack");
await feed.addActivity({
  type: "post",
  text: "I love this movie!",
  collection_refs: ["movies:lord_of_the_rings"],
});

// Read the feed and see the enriched collection data
const response = await feed.getOrCreate({});
console.log(response);
/*
{
    "activities": [
        {
            "type": "post",
            "text": "I love this movie!",
            "collections": {
                "movies:lord_of_the_rings": {
                    "name": "movies",
                    "id": "lord_of_the_rings",
                    "custom": {
                        "title": "Lord of the Rings",
                        "genre": "fantasy",
                        "rating": 9
                    },
                    "user_id": "jack",
                    "created_at": "2025-01-01T00:00:00.000Z",
                    "updated_at": "2025-01-01T00:00:00.000Z",
                    "status": "ok"
                }
            }
        }
    ],
    ...
}
*/
```

```js label="React"
// Create a collection
await client.createCollections({
  collections: [
    {
      name: "movies",
      id: "lord_of_the_rings",
      custom: {
        title: "Lord of the Rings",
        genre: "fantasy",
        rating: 9,
      },
    },
  ],
});

// Add the reference to an activity
const feed = client.feed("user", "jack");
await feed.addActivity({
  type: "post",
  text: "I love this movie!",
  collection_refs: ["movies:lord_of_the_rings"],
});

// Read the feed and see the enriched collection data
const response = await feed.getOrCreate({});
console.log(response);
/*
{
    "activities": [
        {
            "type": "post",
            "text": "I love this movie!",
            "collections": {
                "movies:lord_of_the_rings": {
                    "name": "movies",
                    "id": "lord_of_the_rings",
                    "custom": {
                        "title": "Lord of the Rings",
                        "genre": "fantasy",
                        "rating": 9
                    },
                    "user_id": "jack",
                    "created_at": "2025-01-01T00:00:00.000Z",
                    "updated_at": "2025-01-01T00:00:00.000Z",
                    "status": "ok"
                }
            }
        }
    ],
    ...
}
*/
```

```js label="React Native"
// Create a collection
await client.createCollections({
  collections: [
    {
      name: "movies",
      id: "lord_of_the_rings",
      custom: {
        title: "Lord of the Rings",
        genre: "fantasy",
        rating: 9,
      },
    },
  ],
});

// Add the reference to an activity
const feed = client.feed("user", "jack");
await feed.addActivity({
  type: "post",
  text: "I love this movie!",
  collection_refs: ["movies:lord_of_the_rings"],
});

// Read the feed and see the enriched collection data
const response = await feed.getOrCreate({});
console.log(response);
/*
{
    "activities": [
        {
            "type": "post",
            "text": "I love this movie!",
            "collections": {
                "movies:lord_of_the_rings": {
                    "name": "movies",
                    "id": "lord_of_the_rings",
                    "custom": {
                        "title": "Lord of the Rings",
                        "genre": "fantasy",
                        "rating": 9
                    },
                    "user_id": "jack",
                    "created_at": "2025-01-01T00:00:00.000Z",
                    "updated_at": "2025-01-01T00:00:00.000Z",
                    "status": "ok"
                }
            }
        }
    ],
    ...
}
*/
```

```js label="Node"
// Create a collection
await client.feeds.createCollections({
  collections: [
    {
      name: "movies",
      id: "lord_of_the_rings",
      custom: {
        title: "Lord of the Rings",
        genre: "fantasy",
        rating: 9,
      },
    },
  ],
});

// Add the reference to an activity
const feed = client.feeds.feed("user", "jack");
await feed.addActivity({
  type: "post",
  text: "I love this movie!",
  collection_refs: ["movies:lord_of_the_rings"],
  user_id: "jack",
});

// Read the feed and see the enriched collection data
const response = await feed.getOrCreate({ user_id: "jack" });
console.log(response);
/*
{
    "activities": [
        {
            "type": "post",
            "text": "I love this movie!",
            "collections": {
                "movies:lord_of_the_rings": {
                    "name": "movies",
                    "id": "lord_of_the_rings",
                    "custom": {
                        "title": "Lord of the Rings",
                        "genre": "fantasy",
                        "rating": 9
                    },
                    "user_id": "jack",
                    "created_at": "2025-01-01T00:00:00.000Z",
                    "updated_at": "2025-01-01T00:00:00.000Z",
                    "status": "ok"
                }
            }
        }
    ],
    ...
}
*/
```

```dart label="Dart"
// Create a collection
final createResponse = await client.createCollections(
  collections: [
    const CollectionRequest(
      name: 'movies',
      id: 'lord_of_the_rings',
      custom: {
        'title': 'Lord of the Rings',
        'genre': 'fantasy',
        'rating': 9,
      },
    ),
  ],
);

// Add the reference to an activity
final feed = client.feed(group: 'user', id: 'jack');
final activityResponse = await feed.addActivity(
  request: const FeedAddActivityRequest(
    type: 'post',
    text: 'I love this movie!',
    collectionRefs: ['movies:lord_of_the_rings'],
  ),
);

// Read the feed and see the enriched collection data
await feed.getOrCreate();
final activities = feed.state.activities;
// The activities will contain enriched collection data in the collections field
// Example response structure:
// {
//     "activities": [
//         {
//             "type": "post",
//             "text": "I love this movie!",
//             "collections": {
//                 "movies:lord_of_the_rings": {
//                     "name": "movies",
//                     "id": "lord_of_the_rings",
//                     "custom": {
//                         "title": "Lord of the Rings",
//                         "genre": "fantasy",
//                         "rating": 9
//                     },
//                     "user_id": "jack",
//                     "created_at": "2025-01-01T00:00:00.000Z",
//                     "updated_at": "2025-01-01T00:00:00.000Z",
//                     "status": "ok"
//                 }
//             }
//         }
//     ],
//     ...
// }
```

```go label="Go"
// Create a collection
_, err := client.Feeds().CreateCollections(context.Background(), &getstream.CreateCollectionsRequest{
	Collections: []getstream.CollectionRequest{
		{
			Name: "movies",
			ID:   "lord_of_the_rings",
			Custom: map[string]any{
				"title":  "Lord of the Rings",
				"genre":  "fantasy",
				"rating": 9,
			},
		},
	},
})
if err != nil {
	log.Fatal("Error creating collections:", err)
}

// Add the reference to an activity
feedsClient := client.Feeds()
feed := feedsClient.Feed("user", "jack")
_, err = feedsClient.AddActivity(context.Background(), &getstream.AddActivityRequest{
	Type:          "post",
	Feeds:         []string{"user:jack"},
	Text:          getstream.PtrTo("I love this movie!"),
	UserID:        getstream.PtrTo("jack"),
	CollectionRefs: []string{"movies:lord_of_the_rings"},
})
if err != nil {
	log.Fatal("Error adding activity:", err)
}

// Read the feed and see the enriched collection data
response, err := feed.GetOrCreate(context.Background(), &getstream.GetOrCreateFeedRequest{
	UserID: getstream.PtrTo("jack"),
})
if err != nil {
	log.Fatal("Error reading feed:", err)
}
activities := response.Data.Activities
// The activities will contain enriched collection data in the collections field
// Example response structure:
// {
//     "activities": [
//         {
//             "type": "post",
//             "text": "I love this movie!",
//             "collections": {
//                 "movies:lord_of_the_rings": {
//                     "name": "movies",
//                     "id": "lord_of_the_rings",
//                     "custom": {
//                         "title": "Lord of the Rings",
//                         "genre": "fantasy",
//                         "rating": 9
//                     },
//                     "user_id": "jack",
//                     "created_at": "2025-01-01T00:00:00.000Z",
//                     "updated_at": "2025-01-01T00:00:00.000Z",
//                     "status": "ok"
//                 }
//             }
//         }
//     ],
//     ...
// }
```

```java label="Java"
// Create a collection
Map<String, Object> customData = new HashMap<>();
customData.put("title", "Lord of the Rings");
customData.put("genre", "fantasy");
customData.put("rating", 9);

CollectionRequest collection = CollectionRequest.builder()
    .name("movies")
    .id("lord_of_the_rings")
    .custom(customData)
    .build();

CreateCollectionsRequest createRequest = CreateCollectionsRequest.builder()
    .collections(List.of(collection))
    .build();

feeds.createCollections(createRequest).execute().getData();

// Add the reference to an activity
AddActivityRequest activity =
    AddActivityRequest.builder()
        .type("post")
        .feeds(List.of("user:jack"))
        .text("I love this movie!")
        .userID("jack")
        .collectionRefs(List.of("movies:lord_of_the_rings"))
        .build();

feeds.addActivity(activity).execute().getData();

// Read the feed and see the enriched collection data
Feed feed = new Feed("user", "jack", feeds);
GetOrCreateFeedRequest feedRequest = GetOrCreateFeedRequest.builder()
    .userID("jack")
    .build();
GetOrCreateFeedResponse feedResponse = feed.getOrCreate(feedRequest).getData();
List<Activity> activities = feedResponse.getActivities();
// The activities will contain enriched collection data in the collections field
// Example response structure:
// {
//     "activities": [
//         {
//             "type": "post",
//             "text": "I love this movie!",
//             "collections": {
//                 "movies:lord_of_the_rings": {
//                     "name": "movies",
//                     "id": "lord_of_the_rings",
//                     "custom": {
//                         "title": "Lord of the Rings",
//                         "genre": "fantasy",
//                         "rating": 9
//                     },
//                     "user_id": "jack",
//                     "created_at": "2025-01-01T00:00:00.000Z",
//                     "updated_at": "2025-01-01T00:00:00.000Z",
//                     "status": "ok"
//                 }
//             }
//         }
//     ],
//     ...
// }
```

```php label="php"
// Create a collection
$collection = new GeneratedModels\CollectionRequest(
    name: 'movies',
    id: 'lord_of_the_rings',
    custom: (object)[
        'title' => 'Lord of the Rings',
        'genre' => 'fantasy',
        'rating' => 9,
    ]
);

$feedsClient->createCollections(
    new GeneratedModels\CreateCollectionsRequest(collections: [$collection])
);

// Add the reference to an activity
$activity = new GeneratedModels\AddActivityRequest(
    type: 'post',
    feeds: ['user:jack'],
    text: 'I love this movie!',
    userID: 'jack',
    collectionRefs: ['movies:lord_of_the_rings']
);
$feedsClient->addActivity($activity);

// Read the feed and see the enriched collection data
$feed = $feedsClient->feed('user', 'jack');
$response = $feed->getOrCreateFeed(
    new GeneratedModels\GetOrCreateFeedRequest(userID: 'jack')
);
$activities = $response->getData()->activities;
// The activities will contain enriched collection data in the collections field
// Example response structure:
// {
//     "activities": [
//         {
//             "type": "post",
//             "text": "I love this movie!",
//             "collections": {
//                 "movies:lord_of_the_rings": {
//                     "name": "movies",
//                     "id": "lord_of_the_rings",
//                     "custom": {
//                         "title": "Lord of the Rings",
//                         "genre": "fantasy",
//                         "rating": 9
//                     },
//                     "user_id": "jack",
//                     "created_at": "2025-01-01T00:00:00.000Z",
//                     "updated_at": "2025-01-01T00:00:00.000Z",
//                     "status": "ok"
//                 }
//             }
//         }
//     ],
//     ...
// }
```

```csharp label="C#"
// Create a collection
var collection = new CollectionRequest
{
    Name = "movies",
    ID = "lord_of_the_rings",
    Custom = new Dictionary<string, object>
    {
        { "title", "Lord of the Rings" },
        { "genre", "fantasy" },
        { "rating", 9 }
    }
};

await _feedsV3Client.CreateCollectionsAsync(
    new CreateCollectionsRequest { Collections = new List<CollectionRequest> { collection } }
);

// Add the reference to an activity
var activity = new AddActivityRequest
{
    Type = "post",
    Text = "I love this movie!",
    UserID = "jack",
    Feeds = new List<string> { "user:jack" },
    CollectionRefs = new List<string> { "movies:lord_of_the_rings" }
};
await _feedsV3Client.AddActivityAsync(activity);

// Read the feed and see the enriched collection data
var feedResponse = await _feedsV3Client.GetOrCreateFeedAsync(
    FeedGroupID: "user",
    FeedID: "jack",
    request: new GetOrCreateFeedRequest { UserID = "jack" }
);
var activities = feedResponse.Activities;
// The activities will contain enriched collection data in the collections field
// Example response structure:
// {
//     "activities": [
//         {
//             "type": "post",
//             "text": "I love this movie!",
//             "collections": {
//                 "movies:lord_of_the_rings": {
//                     "name": "movies",
//                     "id": "lord_of_the_rings",
//                     "custom": {
//                         "title": "Lord of the Rings",
//                         "genre": "fantasy",
//                         "rating": 9
//                     },
//                     "user_id": "jack",
//                     "created_at": "2025-01-01T00:00:00.000Z",
//                     "updated_at": "2025-01-01T00:00:00.000Z",
//                     "status": "ok"
//                 }
//             }
//         }
//     ],
//     ...
// }
```

```python label="Python"
# Create a collection
response = client.feeds.create_collections(
    collections=[
        CollectionRequest(
            name="movies",
            id="lord_of_the_rings",
            custom={
                "title": "Lord of the Rings",
                "genre": "fantasy",
                "rating": 9,
            }
        )
    ]
)

# Add the reference to an activity
response = client.feeds.add_activity(
    type="post",
    feeds=["user:jack"],
    text="I love this movie!",
    user_id="jack",
    collection_refs=["movies:lord_of_the_rings"]
)

# Read the feed and see the enriched collection data
feed = client.feeds.feed("user", "jack")
feed_response = feed.get_or_create(user_id="jack")
activities = feed_response.activities
# The activities will contain enriched collection data in the collections field
# Example response structure:
# {
#     "activities": [
#         {
#             "type": "post",
#             "text": "I love this movie!",
#             "collections": {
#                 "movies:lord_of_the_rings": {
#                     "name": "movies",
#                     "id": "lord_of_the_rings",
#                     "custom": {
#                         "title": "Lord of the Rings",
#                         "genre": "fantasy",
#                         "rating": 9
#                     },
#                     "user_id": "jack",
#                     "created_at": "2025-01-01T00:00:00.000Z",
#                     "updated_at": "2025-01-01T00:00:00.000Z",
#                     "status": "ok"
#                 }
#             }
#         }
#     ],
#     ...
# }
```

```ruby label="Ruby"
# Create a collection
collection = GetStream::Generated::Models::CollectionRequest.new(
  name: 'movies',
  id: 'lord_of_the_rings',
  custom: {
    title: 'Lord of the Rings',
    genre: 'fantasy',
    rating: 9
  }
)

request = GetStream::Generated::Models::CreateCollectionsRequest.new(
  collections: [collection]
)

client.feeds.create_collections(request)

# Add the reference to an activity
activity_request = GetStream::Generated::Models::AddActivityRequest.new(
  type: 'post',
  text: 'I love this movie!',
  user_id: 'jack',
  feeds: ['user:jack'],
  collection_refs: ['movies:lord_of_the_rings']
)

client.feeds.add_activity(activity_request)

# Read the feed and see the enriched collection data
feed = client.feed('user', 'jack')
feed_request = GetStream::Generated::Models::GetOrCreateFeedRequest.new(user_id: 'jack')
feed_response = feed.get_or_create_feed(feed_request)
activities = feed_response.activities
# The activities will contain enriched collection data in the collections field
# Example response structure:
# {
#     "activities": [
#         {
#             "type": "post",
#             "text": "I love this movie!",
#             "collections": {
#                 "movies:lord_of_the_rings": {
#                     "name": "movies",
#                     "id": "lord_of_the_rings",
#                     "custom": {
#                         "title": "Lord of the Rings",
#                         "genre": "fantasy",
#                         "rating": 9
#                     },
#                     "user_id": "jack",
#                     "created_at": "2025-01-01T00:00:00.000Z",
#                     "updated_at": "2025-01-01T00:00:00.000Z",
#                     "status": "ok"
#                 }
#             }
#         }
#     ],
#     ...
# }
```

</Tabs>

<admonition type="info">

The enrichment is a best effort process. Missing or deleted collections will not be enriched but will be retured with a status of `notfound`

</admonition>


---

This page was last updated at 2026-04-22T16:42:41.835Z.

For the most recent version of this documentation, visit [https://getstream.io/activity-feeds/docs/node/collections/](https://getstream.io/activity-feeds/docs/node/collections/).