GET /content/:id

The GET /content/:id endpoint allows you to check the status of a previously submitted content request and retrieve the results when processing is complete. This endpoint is used for polling the status of your request until it's ready.

Endpoint

GET https://www.trendifi.app/api/v1/content/:id

Path Parameters

ParameterTypeDescription
idstringThe unique identifier of the content request (returned from POST /content)

Headers

HeaderRequiredDescription
AuthorizationYesYour TrendiFi API key as a Bearer token (e.g., 'Bearer YOUR_API_KEY')

Response

The response from the GET /content/:id endpoint will vary depending on the current status of the content request. There are three possible status values: "processing", "completed", and "failed".

Processing Status Response

When the content is still being processed, the response will include the status:

{
  "id": "ins_01H9XK7ABCDEFGHIJKLMNOPQRS",
  "status": "processing",
  "created_at": "2023-09-15T14:23:45Z"
}

Completed Status Response

When the content processing is complete, the response will include the full content data:

{
  "id": "ins_01H9XK7ABCDEFGHIJKLMNOPQRS",
  "status": "completed",
  "created_at": "2023-09-15T14:23:45Z",
  "completed_at": "2023-09-15T14:24:15Z",
  "result": {
    "topic": "AI trends in healthcare",
    "insight": "AI is transforming healthcare through several key trends...

... (450 words, truncated)"
  }
}

Polling Best Practices

When polling for the status of a content request, follow these best practices to ensure efficient usage of the API:

  1. Implement exponential backoff - Start with a short interval between requests (e.g., 3 second) and gradually increase it to avoid overwhelming the API.
  2. Set a maximum polling interval - Cap the interval at a reasonable maximum (e.g., 10 seconds) to ensure timely updates.

Example Polling Implementation

Here are examples of how to implement polling with exponential backoff in different programming languages:

// Node.js polling example
async function pollContent(contentId) {
  const apiKey = 'YOUR_API_KEY';
  const apiUrl = 'https://trendifi.app/api/v1';
  let delay = 3000; // Start with 3 second delay to avoid rate limiting
  
  while (true) {
    try {
      const response = await fetch(apiUrl + '/content/' + contentId, {
        headers: {
          'Authorization': 'Bearer ' + apiKey
        }
      });
      
      if (!response.ok) {
        throw new Error('HTTP error! status: ' + response.status);
      }
      
      const data = await response.json();
      
      if (data.status === 'completed') {
        console.log('Content is ready!');
        return data.result;
      } else if (data.status === 'failed') {
        throw new Error('Content processing failed: ' + data.error.message);
      }
      
      // Still processing, wait and try again
      console.log('Content still processing...');
      delay = Math.min(delay * 1.5, 30000); // Max delay of 30 seconds
      await new Promise(resolve => setTimeout(resolve, delay));
    } catch (error) {
      console.error('Error polling content:', error);
      throw error;
    }
  }
}

Error Handling

When errors occur during API calls, TrendiFi returns standard HTTP status codes along with detailed error information. For most error cases, you'll receive a structured error response with an error code that helps identify the specific issue.

TrendiFi's API uses standard HTTP status codes to indicate the success or failure of requests. Note: When errors occur, tokens are not deducted from your account.

⚠️ Important:

When handling errors, always check the HTTP status code first, then examine the JSON error object. This is especially critical for 400-404 status codes, as they provide specific information about what went wrong.

Status CodeDescription
200 OKThe request was successful
400 Bad RequestThe request was invalid or missing required parameters
401 UnauthorizedInvalid or missing API key
402 Payment RequiredAccount has insufficient tokens (0 tokens in balance)
404 Not FoundThe requested resource does not exist
405 Method Not AllowedThe HTTP method used is not supported for this endpoint (e.g., using GET instead of POST)
429 Too Many RequestsRate limit exceeded
500 Internal Server ErrorAn error occurred on the server

Error responses include a JSON body with an error object containing a message and an error code:

{
  "error": {
    "message": "No topic provided",
    "error_code": "missing_topic"
  }
}

When a requested resource is not found, you'll receive a 404 status code with a specific error message:

{
  "error": {
    "code": "not_found",
    "message": "Insight with ID 'ins_01H9XK7ABCDEFGHIJKLMNOPQRS' not found"
  }
}

Error Codes

The following table lists all possible error codes that may be returned by the TrendiFi API. Important: When errors occur, tokens are not deducted from your account.

Error CodeDescriptionHTTP Status
missing_topicNo topic was provided in the request400
missing_data_structureNo data structure was provided for a custom_json format request400
invalid_data_structureThe data structure provided is invalid or malformed400
unauthorizedInvalid or missing API key401
insufficient_tokensThe account does not have enough tokens to process the request402
not_foundThe specified resource (content/insight) ID does not exist404
content_not_foundThe specified content ID does not exist (legacy code)404
endpoint_not_foundThe requested API endpoint does not exist404
incorrect_methodThe HTTP method used is not supported for this endpoint405
rate_limit_exceededThe API rate limit has been exceeded429
internal_server_errorAn unexpected error occurred on the server500