Skip to main content

Creating and Managing Content

In a social media app, content creation and management are crucial features. Tapestry provides powerful endpoints to handle these operations efficiently. This guide will walk you through creating, updating, and deleting posts using the socialfi package or the API directly. We recommend using the socialfi package as it's simpler and handles much of the complexity for you.

We recommend using the socialfi package for easier integration:

npm install socialfi
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 Post

The first step in most social media apps is allowing users to create posts. This is achieved through the content creation endpoint.

Using the Socialfi Package

Here's an example of how to create a new post:

try {
const newPost = await client.contents.findOrCreateCreate(
{
apiKey: API_KEY,
},
{
profileId: 'user123',
content: 'Hello world! This is my first post on Tapestry.',
contentType: 'text',
customProperties: [
{
key: 'title',
value: 'My First Post'
},
{
key: 'url',
value: 'example.com/post1'
},
{
key: 'tags',
value: 'introduction,hello-world'
}
],
blockchain: 'SOLANA',
execution: 'FAST_UNCONFIRMED'
}
);
console.log('Post created:', newPost);
} catch (error) {
console.error('Error creating post:', error);
}

Using the API Directly

If you prefer to call the API directly:

try {
const response = await fetch('https://api.usetapestry.dev/v1/contents/create?apiKey=YOUR_API_KEY', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
profileId: 'user123',
content: 'Hello world! This is my first post on Tapestry.',
contentType: 'text',
customProperties: [
{
key: 'title',
value: 'My First Post'
},
{
key: 'url',
value: 'example.com/post1'
},
{
key: 'tags',
value: 'introduction,hello-world'
}
],
blockchain: 'SOLANA',
execution: 'FAST_UNCONFIRMED'
}),
});

const newPost = await response.json();
console.log('Post created:', newPost);
} catch (error) {
console.error('Error creating post:', error);
}

In this example:

  • profileId represents the user creating the post
  • content is the main text content of the post
  • customProperties is an array of key-value pairs for additional metadata
  • contentType specifies the type of content (text, image, video, etc.)

Reading Posts

To retrieve posts, you can use various methods:

Get a specific post by ID

Using Socialfi Package

try {
const post = await client.contents.getContent(
{
apiKey: API_KEY,
contentId: 'post123'
}
);
console.log('Post data:', post);
} catch (error) {
console.error('Error fetching post:', error);
}

Using the API Directly

try {
const response = await fetch(
'https://api.usetapestry.dev/v1/contents/post123?apiKey=YOUR_API_KEY'
);
const post = await response.json();
console.log('Post data:', post);
} catch (error) {
console.error('Error fetching post:', error);
}

Get posts by a specific user

Using Socialfi Package

try {
const userPosts = await client.contents.getContentsByProfile(
{
apiKey: API_KEY,
profileId: 'user123',
limit: 10,
offset: 0
}
);
console.log('User posts:', userPosts);
} catch (error) {
console.error('Error fetching user posts:', error);
}

Using the API Directly

try {
const response = await fetch(
'https://api.usetapestry.dev/v1/contents/profile/user123?apiKey=YOUR_API_KEY&limit=10&offset=0'
);
const userPosts = await response.json();
console.log('User posts:', userPosts);
} catch (error) {
console.error('Error fetching user posts:', error);
}

Updating a Post

Sometimes users may want to edit their posts. You can use the content update endpoint for this purpose:

Using Socialfi Package

try {
const updatedPost = await client.contents.updateContent(
{
apiKey: API_KEY,
},
{
id: 'post123',
content: 'Updated content: Hello, world! This is my edited post.',
customProperties: [
{
key: 'title',
value: 'My Updated Post'
},
{
key: 'edited',
value: 'true'
}
],
blockchain: 'SOLANA',
execution: 'FAST_UNCONFIRMED'
}
);
console.log('Post updated:', updatedPost);
} catch (error) {
console.error('Error updating post:', error);
}

Using the API Directly

try {
const response = await fetch('https://api.usetapestry.dev/v1/contents/update?apiKey=YOUR_API_KEY', {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
id: 'post123',
content: 'Updated content: Hello, world! This is my edited post.',
customProperties: [
{
key: 'title',
value: 'My Updated Post'
},
{
key: 'edited',
value: 'true'
}
],
blockchain: 'SOLANA',
execution: 'FAST_UNCONFIRMED'
}),
});

const updatedPost = await response.json();
console.log('Post updated:', updatedPost);
} catch (error) {
console.error('Error updating post:', error);
}

Here, id is the unique identifier of the post you want to update.

Deleting a Post

To allow users to delete their posts, use the content deletion endpoint:

Using Socialfi Package

try {
const deletionResult = await client.contents.deleteContent(
{
apiKey: API_KEY,
},
{
id: 'post123',
blockchain: 'SOLANA',
execution: 'FAST_UNCONFIRMED'
}
);
console.log('Post deleted:', deletionResult);
} catch (error) {
console.error('Error deleting post:', error);
}

Using the API Directly

try {
const response = await fetch('https://api.usetapestry.dev/v1/contents/delete?apiKey=YOUR_API_KEY', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
id: 'post123',
blockchain: 'SOLANA',
execution: 'FAST_UNCONFIRMED'
}),
});

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

Again, id is the unique identifier of the post to be deleted.

Execution Methods

When creating, updating, or deleting content, you can specify an execution method. This determines how the transaction is processed on the blockchain. For more information on execution methods, see our Execution Methods documentation.

The available execution methods are:

  • FAST_UNCONFIRMED - Fastest response time (default)
  • QUICK_SIGNATURE - Returns transaction signature
  • CONFIRMED_AND_PARSED - Waits for full blockchain confirmation

Example Response Structure

Here's what a typical content response looks like:

{
"content": {
"id": "post123",
"profileId": "user123",
"content": "Hello world! This is my first post on Tapestry.",
"contentType": "text",
"blockchain": "SOLANA",
"customProperties": {
"title": "My First Post",
"url": "example.com/post1",
"tags": "introduction,hello-world"
},
"createdAt": "2024-01-15T10:30:00Z",
"updatedAt": "2024-01-15T10:30:00Z"
},
"engagement": {
"likes": 0,
"comments": 0,
"shares": 0
}
}

Best Practices

By leveraging these content endpoints with the socialfi package or API directly, you can build a robust and interactive social media application on Tapestry. Remember to:

  • Handle errors gracefully and provide user feedback
  • Implement appropriate user interfaces for a seamless experience
  • Use custom properties to store additional metadata
  • Consider rate limiting and API usage best practices
  • Validate user input before sending to the API