Practical examples of using the Basalt SDK for prompt management.
This guide provides practical examples of using the Basalt SDK for common prompt management scenarios. These examples demonstrate how to use the get, list, and describe methods effectively in your applications.
async function findPromptsByTag(tagName: string) { const { value, error } = await basalt.prompt.list() if (error) { console.error('Failed to list prompts:', error.message) return [] } // Find prompts that have the specified tag available const promptsWithTag = value.filter(prompt => prompt.availableTags.includes(tagName) ) console.log(`Found ${promptsWithTag.length} prompts with tag "${tagName}"`) return promptsWithTag}
async function getFeatureFlaggedPrompt(promptSlug: string) { // Get user segment (e.g., from user profile) const userSegment = await getUserSegment() // Map user segment to prompt tag let promptTag switch (userSegment) { case 'beta-tester': promptTag = 'beta' break case 'premium': promptTag = 'premium' break default: promptTag = 'standard' } // Get the appropriate prompt variant using tags const { value, error } = await basalt.prompt.get({ slug: promptSlug, tag: promptTag }) if (error) { // Fall back to standard prompt if specific tag doesn't exist if (error.name === 'NotFoundError' && promptTag !== 'standard') { console.warn(`Tag ${promptTag} not found for ${promptSlug}, falling back to standard`) return getFeatureFlaggedPrompt('standard') } console.error(`Failed to get prompt ${promptSlug} with tag ${promptTag}:`, error.message) return null } return value}