Integration Examples

The Integration Examples section is designed to provide practical guidance for using the Eden AI API in various programming environments.

Sample Code Snippets in Various Programming Languages

To accommodate developers working in different languages, Eden AI offers sample code snippets for popular programming languages.

Workflow with text inputs

import json
import requests

headers = {"Authorization": "Bearer your_API_key"}

url = "https://api.edenai.run/v2/workflow/{workflow_id}/execution/"

payload = {"subject":"<string value>","primary_keyword":"<string value>","secondary_keywords":"<string value>"}

response = requests.post(url, json=payload, headers=headers)
result = json.loads(response.text)
import fetch from "node-fetch"; // Only needed for Node.js, not needed in the browser

const url = "https://api.edenai.run/v2/workflow/{workflow_id}/execution/"
const payload = {"subject":"<string value>","primary_keyword":"<string value>","secondary_keywords":"<string value>"}

const launchExecution = async () => {
  const response = await fetch(url, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      "Authorization": "Bearer your_API_key"
    },
    body: JSON.stringify(payload)
  });
  const result = await response.json()
  return result;
}
const data = await launchExecution();
<?php

$url = "https://api.edenai.run/v2/workflow/{workflow_id}/execution/";

// Replace "your_API_key" with your actual API key
$headers = [
    "Authorization: Bearer your_API_key",
    "Content-Type: application/json"
];

// Define the payload
$payload = [
    "subject" => "<string value>",
    "primary_keyword" => "<string value>",
    "secondary_keywords" => "<string value>"
];

// Initialize cURL
$ch = curl_init();

// Set cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute the request
$response = curl_exec($ch);

// Check for errors
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}

// Close the cURL session
curl_close($ch);

// Decode the JSON response
$result = json_decode($response, true);

// Print the result
print_r($result);
?>
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

class Program
{
    static async Task Main(string[] args)
    {
        string url = "https://api.edenai.run/v2/workflow/{workflow_id}/execution/";
        string apiKey = "your_API_key";

        // Payload
        var payload = new
        {
            subject = "<string value>",
            primary_keyword = "<string value>",
            secondary_keywords = "<string value>"
        };

        // Convert payload to JSON
        string jsonPayload = JsonConvert.SerializeObject(payload);

        // Create an HTTP client
        using (HttpClient client = new HttpClient())
        {
            // Add the Authorization header
            client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");

            // Prepare the HTTP POST request
            var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");

            try
            {
                // Send the request
                HttpResponseMessage response = await client.PostAsync(url, content);

                // Ensure the response was successful
                response.EnsureSuccessStatusCode();

                // Read the response content
                string responseContent = await response.Content.ReadAsStringAsync();

                // Deserialize JSON response
                var result = JsonConvert.DeserializeObject(responseContent);

                // Print the result
                Console.WriteLine(JsonConvert.SerializeObject(result, Formatting.Indented));
            }
            catch (Exception ex)
            {
                // Handle errors
                Console.WriteLine($"Error: {ex.Message}");
            }
        }
    }
}

Workflow with file input

import json
import requests

headers = {"Authorization": "Bearer your_API_key"}

url = "https://api.edenai.run/v2/workflow/{workflow_id}/execution/"
files = {"doc_1":open('/path/to/your/doc.pdf', 'rb')} 


response = requests.post(url, files=files, headers=headers)
result = json.loads(response.text)
import fs from 'fs'; // Only needed for Node.js to read the file
import fetch from "node-fetch"; // Only needed for Node.js, not needed in the browser. npm install node-fetch if not installed
import FormData from 'form-data'; // npm install --save form-data is not installed

const url = "https://api.edenai.run/v2/workflow/{workflow_id}/execution/"

const formData = new FormData();

// Append your files
  formData.append('file', fs.createReadStream('/path/to/your/doc.pdf'), 'file.ext');
  


const launchExecution = async () => {
  const response = await fetch(url, {
    method: "POST",
    headers: {
      ...formData.getHeaders(), // Add FormData headers
      "Authorization": "Bearer your_API_key"
    },
    body: formData
  });
  const result = await response.json()
  return result;
}
const data = await launchExecution();
<?php

$url = "https://api.edenai.run/v2/workflow/{workflow_id}/execution/";

// Replace "your_API_key" with your actual API key
$headers = [
    "Authorization: Bearer your_API_key"
];

// File path to upload
$filePath = "/path/to/your/doc.pdf";

// Initialize cURL
$ch = curl_init();

// Prepare the file data for multipart/form-data
$postFields = [
    "doc_1" => new CURLFile($filePath)
];

// Set cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute the request
$response = curl_exec($ch);

// Check for errors
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}

// Close the cURL session
curl_close($ch);

// Decode the JSON response
$result = json_decode($response, true);

// Print the result
print_r($result);
?>
curl -X POST "https://api.edenai.run/v2/workflow/{workflow_id}/execution/" \
-H "Authorization: Bearer your_API_key" \
-F "doc_1=@/path/to/your/doc.pdf"
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Newtonsoft.Json;

class Program
{
    static async Task Main(string[] args)
    {
        string url = "https://api.edenai.run/v2/workflow/{workflow_id}/execution/";
        string apiKey = "your_API_key";
        string filePath = "/path/to/your/doc.pdf";

        // Create an HTTP client
        using (HttpClient client = new HttpClient())
        {
            // Add the Authorization header
            client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");

            // Prepare the file content
            using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                using (var content = new MultipartFormDataContent())
                {
                    // Add the file
                    var fileContent = new StreamContent(fileStream);
                    fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/pdf");
                    content.Add(fileContent, "doc_1", Path.GetFileName(filePath));

                    try
                    {
                        // Send the POST request
                        HttpResponseMessage response = await client.PostAsync(url, content);

                        // Ensure the response was successful
                        response.EnsureSuccessStatusCode();

                        // Read the response content
                        string responseContent = await response.Content.ReadAsStringAsync();

                        // Deserialize JSON response
                        var result = JsonConvert.DeserializeObject(responseContent);

                        // Print the result
                        Console.WriteLine(JsonConvert.SerializeObject(result, Formatting.Indented));
                    }
                    catch (Exception ex)
                    {
                        // Handle errors
                        Console.WriteLine($"Error: {ex.Message}");
                    }
                }
            }
        }
    }
}

Workflow with both file and text inputs

import json
import requests

headers = {"Authorization": "Bearer your_API_key"}

url = "https://api.edenai.run/v2/workflow/{workflow_id}/execution/"
files = {"audio_recording":open('/path/to/your/audio_recording.mp3', 'rb')} 
payload = {"medical_role":"<string value>"}

response = requests.post(url, files=files, data=payload, headers=headers)
result = json.loads(response.text)

import fs from 'fs'; // Only needed for Node.js to read the file
import fetch from "node-fetch"; // Only needed for Node.js, not needed in the browser. npm install node-fetch if not installed
import FormData from 'form-data'; // npm install --save form-data is not installed

const url = "https://api.edenai.run/v2/workflow/{workflow_id}/execution/"

const formData = new FormData();

// Append your files
  formData.append('audio_recording', fs.createReadStream('/path/to/your/audio_recording.mp3'), 'audio_recording.ext');
  
// Append your data
  formData.append('medical_role', '<string value>');
  

const launchExecution = async () => {
  const response = await fetch(url, {
    method: "POST",
    headers: {
      ...formData.getHeaders(), // Add FormData headers
      "Authorization": "Bearer your_API_key"
    },
    body: formData
  });
  const result = await response.json()
  return result;
}
const data = await launchExecution();
<?php

$url = "https://api.edenai.run/v2/workflow/{workflow_id}/execution/";

// Replace "your_API_key" with your actual API key
$headers = [
    "Authorization: Bearer your_API_key"
];

// File path to upload
$filePath = "/path/to/your/audio_recording.mp3";

// Payload data
$payload = [
    "medical_role" => "<string value>"
];

// Initialize cURL
$ch = curl_init();

// Prepare the file data for multipart/form-data
$postFields = array_merge(
    $payload,
    ["audio_recording" => new CURLFile($filePath)]
);

// Set cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute the request
$response = curl_exec($ch);

// Check for errors
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}

// Close the cURL session
curl_close($ch);

// Decode the JSON response
$result = json_decode($response, true);

// Print the result
print_r($result);
?>
curl -X POST "https://api.edenai.run/v2/workflow/{workflow_id}/execution/" \
-H "Authorization: Bearer your_API_key" \
-F "audio_recording=@/path/to/your/audio_recording.mp3" \
-F "medical_role=<string value>"
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Newtonsoft.Json;

class Program
{
    static async Task Main(string[] args)
    {
        string url = "https://api.edenai.run/v2/workflow/{workflow_id}/execution/";
        string apiKey = "your_API_key";
        string audioFilePath = "/path/to/your/audio_recording.mp3";

        // Define the payload
        var payload = new MultipartFormDataContent
        {
            { new StringContent("<string value>"), "medical_role" }
        };

        // Add the file
        using (var fileStream = new FileStream(audioFilePath, FileMode.Open, FileAccess.Read))
        {
            var fileContent = new StreamContent(fileStream);
            fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("audio/mpeg");
            payload.Add(fileContent, "audio_recording", Path.GetFileName(audioFilePath));
        }

        // Create an HTTP client
        using (HttpClient client = new HttpClient())
        {
            // Add the Authorization header
            client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");

            try
            {
                // Send the POST request
                HttpResponseMessage response = await client.PostAsync(url, payload);

                // Ensure the response was successful
                response.EnsureSuccessStatusCode();

                // Read the response content
                string responseContent = await response.Content.ReadAsStringAsync();

                // Deserialize JSON response
                var result = JsonConvert.DeserializeObject(responseContent);

                // Print the result
                Console.WriteLine(JsonConvert.SerializeObject(result, Formatting.Indented));
            }
            catch (Exception ex)
            {
                // Handle errors
                Console.WriteLine($"Error: {ex.Message}");
            }
        }
    }
}

Get execution result


import json
import requests

headers = {"Authorization": "Bearer your_API_key"}

url = "https://api.edenai.run/v2/workflow/{workflow_id}/execution/{execution_id}/"

response = requests.get(url, headers=headers)
result = json.loads(response.text)
              

import fetch from "node-fetch"; // Only needed for Node.js, not needed in the browser. npm install node-fetch if not installed

const url = "https://api.edenai.run/v2/workflow/{workflow_id}/execution/{execution_id}/"

const getExecution = async () => {
  const response = await fetch(url, {
    headers: {
      "Content-Type": "application/json",
      "Authorization": "Bearer your_API_key"
    },
  });
  const result = await response.json();
  return result;
}
const data = await getExecution();
<?php

$url = "https://api.edenai.run/v2/workflow/{workflow_id}/execution/{execution_id}/";

// Replace "your_API_key" with your actual API key
$headers = [
    "Authorization: Bearer your_API_key"
];

// Initialize cURL
$ch = curl_init();

// Set cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute the GET request
$response = curl_exec($ch);

// Check for errors
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}

// Close the cURL session
curl_close($ch);

// Decode the JSON response
$result = json_decode($response, true);

// Print the result
print_r($result);
?>

curl -X GET "https://api.edenai.run/v2/workflow/{workflow_id}/execution/{execution_id}/" \
-H "Authorization: Bearer your_API_key"
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;

class Program
{
    static async Task Main(string[] args)
    {
        string url = "https://api.edenai.run/v2/workflow/{workflow_id}/execution/{execution_id}/";
        string apiKey = "your_API_key";

        // Create an HTTP client
        using (HttpClient client = new HttpClient())
        {
            // Add the Authorization header
            client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");

            try
            {
                // Send the GET request
                HttpResponseMessage response = await client.GetAsync(url);

                // Ensure the response was successful
                response.EnsureSuccessStatusCode();

                // Read the response content
                string responseContent = await response.Content.ReadAsStringAsync();

                // Deserialize JSON response
                var result = JsonConvert.DeserializeObject(responseContent);

                // Print the result
                Console.WriteLine(JsonConvert.SerializeObject(result, Formatting.Indented));
            }
            catch (Exception ex)
            {
                // Handle errors
                Console.WriteLine($"Error: {ex.Message}");
            }
        }
    }
}