// Get the document export result in JSON format var exports = Context.Transaction.Documents[0].Exports; var extractedDataExportResult = exports.find(element => element.ExportFormat === ExportFormat.FieldsJson); // Ensure there's extracted data in JSON format if (!extractedDataExportResult) { Context.LogMessage("No extracted data found in JSON format."); // Instead of using 'return', simply exit the script by skipping the rest of the logic. } else { // Claude API parameters const apiKey = Context.GetSecret("Calude_API_key"); // Replace with your actual API key const model = "claude-3-sonnet-20240229"; const prompt = "I am in forign country and I don't want to get scammed. I am giving you extracted JSON file data of an invoice please check for any issues or fraud in terms on money"; const maxTokens = 1024; // Send the extracted JSON data to Claude for analysis try { // Create request to send the JSON payload to Claude const request = Context.CreateHttpRequest(); request.Url = "https://api.anthropic.com/v1/messages"; request.Method = "POST"; request.AuthScheme = "Bearer"; request.AuthToken = apiKey; // Using API key for authorization // Construct the prompt by embedding the JSON data const payload = { model: model, max_tokens: maxTokens, messages: [ { role: "user", content: `${prompt}\n\n${extractedDataExportResult}` } ] }; // Set headers for the API request request.SetHeader("Content-Type", "application/json"); request.SetHeader("x-api-key", apiKey); request.SetHeader("anthropic-version", "2023-06-01"); // Set the request payload with the JSON invoice data request.SetStringContent(JSON.stringify(payload)); // Log the sending process for debugging Context.LogMessage("Sending extracted invoice data to Claude API for analysis."); // Send the request and handle the response request.Send(); // Log response details (for debugging) Context.LogMessage("Response Status: " + request.Status); Context.LogMessage("Response Body: " + request.ResponseText); // Parse and process Claude's response var responseObject = JSON.parse(request.ResponseText); // Log potential issues flagged by Claude Context.LogMessage("Issues flagged by Claude: " + responseObject.content[0].text.trim()); // Save flagged issues to the document field (optional) var flaggedIssuesField = Context.Transaction.Documents[0].GetField("Flagged Issues"); flaggedIssuesField.Value = responseObject.content[0].text.trim(); Context.Transaction.Documents[0].GetField("JSON data").Value = extractedDataExportResult; Context.LogMessage('Invoice data successfully analyzed by Claude.'); } catch (error) { Context.ErrorMessage = `Error sending invoice data to Claude: ${error.message}`; Context.LogMessage("Error details: " + error.toString()); } }