Generate Prompt
The generatePrompt endpoint allows you to send a prompt template to the PromptTK API for processing. This endpoint validates your input, creates a project if needed, and returns the generated prompt or an error if the request fails.
import { PromptTK } from "@devvle/ptk-api-sdk";
const promptTK = new PromptTK({ userId: "your-user-id", apiKey: "your-api-key",});
const promptTemplate = { mainPrompt: "Write a short story about a robot learning to love.", // ...other optional fields};
async function generatePrompt() { const promptName = "My First Prompt"; // Optional: specify a name for the prompt const projectId = "your-project-id"; // Optional: specify a project ID if you have one try { const response = await promptTK.generatePrompt( promptTemplate, promptName, projectId ); const data = await response.json(); console.log("Prompt generated:", data); } catch (error) { console.error("Error generating prompt:", error); }}generatePrompt();Expected Output:
{ success: true, message: 'Prompt generated successfully', data: { promptId: '<your-prompt-id-here>' }}The generatePrompt function makes a request to the following endpoint:
POST /v1/promptsRequest Body
Section titled “Request Body”The request body should be a JSON object with the following structure, you can read more about the fields in the Prompt Input documentation:
{ "input": { "mainPrompt": "string", // required "endUserDescription": "string", // optional "promptSenderDescription": "string", // optional "relationship": "string", // optional "mustHaves": "string", // optional "contextRequirements": "string", // optional "lengthRequirements": "string", // optional "avoidElements": "string", // optional "styleAndTone": ["string"], // optional "examplePrompts": "string", // optional "outputType": "string", // optional "responseFormat": "string", // optional "promptGoal": "string", // optional "hallucinationControl": "string", // optional "projectId": "string" // optional }, "promptName": "string", // optional "projectId": "string" // optional}Headers
Section titled “Headers”| Header | Value |
|---|---|
Content-Type | application/json |
Authorization | Bearer your-api-key |
x-user-id | your-user-id |
Response
Section titled “Response”On success (201 Created):
{ "success": true, "message": "Prompt generated successfully", "data": { "promptId": "<your-prompt-id-here>" }}On error, you may receive:
{ "success": false, "message": "Prompt is required"}or
{ "success": false, "message": "Failed to add prompt to project", "error": "...error message..."}- The endpoint will automatically create a project called “Api Project” for the user if one does not exist, and associate all prompt calls with it unless a
projectIdis provided. - All fields in the
promptobject are optional except formainPrompt.