Skip to main content
GET
/
snippet
Get a single snippet
curl --request GET \
  --url https://www.getsnippets.ai/api/prompts/snippet \
  --header 'Authorization: Bearer <token>'
{
"success": true,
"data": {
"id": "3c90c3cc-0d44-4b50-8888-8dd25736052a",
"title": "<string>",
"content": {
"type": "plaintext",
"content": "Hello, world!"
},
"snippet_note": "<string>",
"shortcut": "<string>",
"folder_id": "3c90c3cc-0d44-4b50-8888-8dd25736052a",
"team_id": "3c90c3cc-0d44-4b50-8888-8dd25736052a",
"workspace_id": "3c90c3cc-0d44-4b50-8888-8dd25736052a",
"is_archived": true,
"created_at": "2023-11-07T05:31:56Z",
"updated_at": "2023-11-07T05:31:56Z"
},
"usage": {
"remainingRequests": 123
}
}

Overview

Retrieves a single snippet by its ID. This endpoint returns the complete snippet data including content, metadata, folder assignment, and timestamps. API Cost: 1 request

Code Examples

const axios = require('axios');

const API_KEY = process.env.SNIPPETS_AI_API_KEY;
const BASE_URL = 'https://www.getsnippets.ai/api/prompts';

async function getSnippet(snippetId) {
	try {
		const response = await axios.get(`${BASE_URL}/snippet`, {
			headers: {
				Authorization: `Bearer ${API_KEY}`,
			},
			params: {
				id: snippetId,
			},
		});

		console.log('Snippet:', response.data.data);
		console.log('Remaining requests:', response.data.usage.remainingRequests);

		return response.data;
	} catch (error) {
		if (error.response) {
			console.error('Error:', error.response.data.message);
			console.error('Status:', error.response.status);
		} else {
			console.error('Error:', error.message);
		}
		throw error;
	}
}

// Usage
getSnippet('550e8400-e29b-41d4-a716-446655440000');

Response Example

{
	"success": true,
	"data": {
		"id": "550e8400-e29b-41d4-a716-446655440000",
		"title": "Welcome Email Template",
		"content": {
			"type": "plaintext",
			"content": "Welcome to our platform! We're excited to have you."
		},
		"snippet_note": "Use for new user onboarding",
		"shortcut": "welcome_email",
		"folder_id": "660e8400-e29b-41d4-a716-446655440001",
		"team_id": "770e8400-e29b-41d4-a716-446655440002",
		"workspace_id": "880e8400-e29b-41d4-a716-446655440003",
		"is_archived": false,
		"created_at": "2024-01-15T10:30:00Z",
		"updated_at": "2024-01-20T14:45:00Z",
		"created_by": "990e8400-e29b-41d4-a716-446655440004"
	},
	"usage": {
		"remainingRequests": 99999
	}
}

Use Cases

Retrieve a specific prompt variation for use in voice AI services like VAPI:
async function getPromptForVoiceAgent(snippetId) {
  const response = await getSnippet(snippetId);
  const promptContent = response.data.content.content;
  
  // Use with VAPI or similar service
  const vapiCall = await initiateVoiceCall({
    systemPrompt: promptContent,
    phoneNumber: customerPhone
  });
  
  return vapiCall;
}
Fetch snippet data to display in your application:
async function displaySnippet(snippetId) {
  const response = await getSnippet(snippetId);
  const snippet = response.data;
  
  // Render in UI
  document.getElementById('snippet-title').textContent = snippet.title;
  document.getElementById('snippet-content').textContent = 
    snippet.content.content;
  document.getElementById('snippet-note').textContent = 
    snippet.snippet_note || 'No notes';
}
Check if a snippet exists before performing operations:
async function safeUpdateSnippet(snippetId, updates) {
  try {
    // Verify snippet exists
    await getSnippet(snippetId);
    
    // Proceed with update
    return await updateSnippet(snippetId, updates);
  } catch (error) {
    if (error.response?.status === 404) {
      console.log('Snippet not found, creating new one');
      return await createSnippet(updates);
    }
    throw error;
  }
}
Implement caching to reduce API calls:
const cache = new Map();
const CACHE_TTL = 5 * 60 * 1000; // 5 minutes

async function getCachedSnippet(snippetId) {
  const cached = cache.get(snippetId);
  
  if (cached && Date.now() - cached.timestamp < CACHE_TTL) {
    console.log('Cache hit - saved 1 API request');
    return cached.data;
  }
  
  const response = await getSnippet(snippetId);
  cache.set(snippetId, {
    data: response.data,
    timestamp: Date.now()
  });
  
  return response.data;
}

Error Handling

async function getSnippetSafely(snippetId) {
	try {
		return await getSnippet(snippetId);
	} catch (error) {
		const status = error.response?.status;
		const message = error.response?.data?.message;

		switch (status) {
			case 400:
				throw new Error(`Invalid request: ${message}`);
			case 401:
				throw new Error('Invalid API key - check your authentication');
			case 403:
				throw new Error('Access denied - check team permissions');
			case 404:
				throw new Error(`Snippet ${snippetId} not found`);
			case 429:
				const retryAfter = error.response.headers['retry-after'];
				throw new Error(`Rate limit exceeded. Retry after ${retryAfter}s`);
			default:
				throw new Error(`API error: ${message || 'Unknown error'}`);
		}
	}
}

Common Issues

Problem: The snippet ID doesn’t exist or was deleted.Solution:
  • Verify the snippet ID is correct
  • Check if the snippet was deleted
  • Ensure the snippet belongs to your workspace
Problem: Your API key doesn’t have access to the snippet’s team. Solution: - Check your API key’s team permissions - Ensure the API key has access to the required team - Verify your workspace subscription is active
Problem: Invalid or missing API key.Solution:
  • Check the Authorization header is properly formatted
  • Verify your API key is active
  • Ensure you’re using Bearer YOUR_API_KEY format

Best Practices

Use caching for frequently accessed snippets to reduce API calls
Implement error handling for all possible error scenarios
Store snippet IDs persistently to avoid lookup operations
Monitor usage through the response’s usage field
Validate IDs before making requests to avoid unnecessary API calls
Don’t fetch snippets in tight loops without caching - this will quickly exhaust your API quota and hit rate limits.

Authorizations

Authorization
string
header
required

API key authentication using Bearer token. Include your API key in the Authorization header.

Query Parameters

id
string<uuid>
required

The ID of the snippet to fetch

Response

Snippet retrieved successfully

success
boolean
Example:

true

data
object
usage
object