On this page

The Comments API allows you to create, update, delete, and retrieve comments associated with content, enabling users to engage in discussions and share their thoughts on posts. You can use either the socialfi package (recommended for easier integration) or call the API directly.

## Installing and Setting Up the Socialfi Package (Recommended)

We recommend using the socialfi package as it simplifies API interactions:

```bash
npm install socialfi
```

```ts
import { SocialFi } from 'socialfi';

const API_URL = 'https://api.usetapestry.dev/v1/'; // tapestry prod URL
// const API_URL = 'https://api.dev.usetapestry.dev/v1/'; // tapestry dev URL

const API_KEY = process.env.TAPESTRY_API_KEY; // Get your API key from https://app.usetapestry.dev/

const client = new SocialFi({
  baseURL: API_URL,
  apiKey: API_KEY,
});
```

## Creating a Comment

To allow users to comment on a post, use the comment creation endpoint:

### Using the Socialfi Package

```ts
try {
  const newComment = await client.comments.createComment(
    {
      apiKey: API_KEY,
    },
    {
      profileId: 'user123', // The ID of the profile creating the comment
      contentId: 'post456', // The ID of the content being commented on
      text: 'Great post! Thanks for sharing.',
      blockchain: 'SOLANA',
      execution: 'FAST_UNCONFIRMED'
    }
  );
  console.log('Comment created:', newComment);
} catch (error) {
  console.error('Error creating comment:', error);
}
```

### Using the API Directly

```ts
try {
  const response = await fetch('https://api.usetapestry.dev/v1/comments?apiKey=YOUR_API_KEY', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      profileId: 'user123',
      contentId: 'post456',
      text: 'Great post! Thanks for sharing.',
      blockchain: 'SOLANA',
      execution: 'FAST_UNCONFIRMED'
    }),
  });

const newComment = await response.json();
  console.log('Comment created:', newComment);
} catch (error) {
  console.error('Error creating comment:', error);
}
```

### Request Parameters

| Field       | Type   | Description                                         |
| ----------- | ------ | --------------------------------------------------- |
| profileId   | string | The ID of the profile creating the comment.        |
| contentId   | string | The ID of the content being commented on.          |
| text        | string | The text content of the comment.                   |
| blockchain  | string | The blockchain to use (default: SOLANA)            |
| execution    | string | Execution method (default: FAST_UNCONFIRMED)       |

### Response Example

```json
{
  "comment": {
    "id": "comment-1234567890",
    "profileId": "user123",
    "contentId": "post456",
    "text": "Great post! Thanks for sharing.",
    "blockchain": "SOLANA",
    "createdAt": "2024-01-15T10:30:00Z",
    "updatedAt": "2024-01-15T10:30:00Z"
  },
  "author": {
    "id": "user123",
    "username": "johndoe",
    "profileImage": "https://example.com/avatar.jpg"
  }
}
```

## Update a Comment

If a user wants to edit their comment, use the comment update endpoint:

### Using the Socialfi Package

```ts
try {
  const updatedComment = await client.comments.updateComment(
    {
      apiKey: API_KEY,
    },
    {
      commentId: 'comment-1234567890',
      text: 'Updated comment: Great post! Thanks for sharing this insight.',
      blockchain: 'SOLANA',
      execution: 'FAST_UNCONFIRMED'
    }
  );
  console.log('Comment updated:', updatedComment);
} catch (error) {
  console.error('Error updating comment:', error);
}
```

### Using the API Directly

```ts
try {
  const response = await fetch('https://api.usetapestry.dev/v1/comments/comment-1234567890?apiKey=YOUR_API_KEY', {
    method: 'PUT',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      text: 'Updated comment: Great post! Thanks for sharing this insight.',
      blockchain: 'SOLANA',
      execution: 'FAST_UNCONFIRMED'
    }),
  });

const updatedComment = await response.json();
  console.log('Comment updated:', updatedComment);
} catch (error) {
  console.error('Error updating comment:', error);
}
```

### Request Parameters

| Field       | Type   | Description                                         |
| ----------- | ------ | --------------------------------------------------- |
| commentId   | string | The ID of the comment to update.                    |
| text        | string | The updated text of the comment.                   |
| blockchain  | string | The blockchain to use                              |
| execution   | string | Execution method                                   |

### Response Example

```json
{
  "comment": {
    "id": "comment-1234567890",
    "profileId": "user123",
    "contentId": "post456",
    "text": "Updated comment: Great post! Thanks for sharing this insight.",
    "blockchain": "SOLANA",
    "createdAt": "2024-01-15T10:30:00Z",
    "updatedAt": "2024-01-15T10:35:00Z"
  }
}
```

## Delete a Comment

To allow users to delete their comments, use the comment deletion endpoint:

### Using the Socialfi Package

```ts
try {
  const deletionResult = await client.comments.deleteComment(
    {
      apiKey: API_KEY,
    },
    {
      commentId: 'comment-1234567890',
      blockchain: 'SOLANA',
      execution: 'FAST_UNCONFIRMED'
    }
  );
  console.log('Comment deleted:', deletionResult);
} catch (error) {
  console.error('Error deleting comment:', error);
}
```

### Using the API Directly

```ts
try {
  const response = await fetch('https://api.usetapestry.dev/v1/comments/comment-1234567890?apiKey=YOUR_API_KEY', {
    method: 'DELETE',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      blockchain: 'SOLANA',
      execution: 'FAST_UNCONFIRMED'
    }),
  });

const deletionResult = await response.json();
  console.log('Comment deleted:', deletionResult);
} catch (error) {
  console.error('Error deleting comment:', error);
}
```

### Response Example

```json
{
  "success": true,
  "message": "Comment deleted successfully"
}
```

## Get Comments by Content

To retrieve comments about a specific post:

### Using the Socialfi Package

```ts
try {
  const comments = await client.comments.getCommentsByContent(
    {
      apiKey: API_KEY,
      contentId: 'post456',
      limit: 20,
      offset: 0
    }
  );
  console.log('Comments for post:', comments);
} catch (error) {
  console.error('Error fetching comments:', error);
}
```

### Using the API Directly

```ts
try {
  const response = await fetch(
    'https://api.usetapestry.dev/v1/comments?contentId=post456&limit=20&offset=0&apiKey=YOUR_API_KEY'
  );
  const comments = await response.json();
  console.log('Comments for post:', comments);
} catch (error) {
  console.error('Error fetching comments:', error);
}
```

### Query Parameters

| Parameter  | Type   | Description                                           |
| ---------- | ------ | ----------------------------------------------------- |
| contentId  | string | The ID of the content to get comments for.          |
| limit      | number | Maximum number of comments to return.                |
| offset     | number | Number of comments to skip.                           |

### Response Example

```json
{
  "comments": [
    {
      "comment": {
        "id": "comment-1234567890",
        "text": "Great content!",
        "createdAt": "2024-01-15T10:30:00Z",
        "updatedAt": "2024-01-15T10:30:00Z"
      },
      "author": {
        "id": "user123",
        "username": "johndoe",
        "profileImage": "https://example.com/avatar1.jpg"
      }
    },
    {
      "comment": {
        "id": "comment-0987654321",
        "text": "Thanks for sharing!",
        "createdAt": "2024-01-15T11:00:00Z",
        "updatedAt": "2024-01-15T11:00:00Z"
      },
      "author": {
        "id": "user789",
        "username": "janedoe",
        "profileImage": "https://example.com/avatar2.jpg"
      }
    }
  ],
  "pagination": {
    "total": 25,
    "limit": 20,
    "offset": 0,
    "hasMore": true
  }
}
```

## Get Comments by Profile

To retrieve comments authored by a specific user:

### Using the Socialfi Package

```ts
try {
  const userComments = await client.comments.getCommentsByProfile(
    {
      apiKey: API_KEY,
      profileId: 'user123',
      limit: 10,
      offset: 0
    }
  );
  console.log('Comments by user:', userComments);
} catch (error) {
  console.error('Error fetching user comments:', error);
}
```

### Using the API Directly

```ts
try {
  const response = await fetch(
    'https://api.usetapestry.dev/v1/comments?profileId=user123&limit=10&offset=0&apiKey=YOUR_API_KEY'
  );
  const userComments = await response.json();
  console.log('Comments by user:', userComments);
} catch (error) {
  console.error('Error fetching user comments:', error);
}
```

### Query Parameters

| Parameter   | Type   | Description                                           |
| ----------- | ------ | ----------------------------------------------------- |
| profileId   | string | The ID of the profile to get comments from.        |
| limit       | number | Maximum number of comments to return.                |
| offset      | number | Number of comments to skip.                           |

### Response Example

```json
{
  "comments": [
    {
      "comment": {
        "id": "comment-1234567890",
        "text": "Great content!",
        "createdAt": "2024-01-15T10:30:00Z",
        "updatedAt": "2024-01-15T10:30:00Z"
      },
      "content": {
        "id": "post456",
        "title": "Amazing Post Title",
        "author": {
          "id": "user789",
          "username": "contentcreator"
        }
      }
    }
  ],
  "pagination": {
    "total": 15,
    "limit": 10,
    "offset": 0,
    "hasMore": true
  }
}
```

## Authentication

All comments endpoints require an API key to be provided in the request for authentication. Make sure to keep your API key secure and never expose it in client-side code.

## Error Handling

In case of an error, the API will respond with an appropriate HTTP status code and an error message in the response body:

```json
{
  "success": false,
  "error": "Invalid comment ID provided",
  "code": "INVALID_COMMENT_ID"
}
```

## Best Practices

When implementing comments functionality:

- Always validate user input before sending to the API
- Implement proper error handling and user feedback
- Consider implementing pagination for better performance
- Use rate limiting to prevent spam
- Validate that users can only edit/delete their own comments
- Consider implementing comment threading for nested discussions
- Implement content moderation as needed
