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.
Synchronous API with text input
import json
import requests
headers = {"Authorization": "Bearer your_API_key"}
url = "https://api.edenai.run/v2/{feature}/{subfeature}"
payload = {"providers":"<provider/model>","text":"<string value>","language":"<string value>"}
response = requests.post(url, json=payload, headers=headers)
result = json.loads(response.text)
const axios = require("axios").default;
const fs = require("fs");
const FormData = require("form-data");
const form = new FormData();
form.append("providers", "revai,voci");
form.append("file", fs.createReadStream("🔊 path/to/your/sound.mp3"));
form.append("language", "en");
const options = {
method: "POST",
url: "https://api.edenai.run/v2/{feature}/{subfeature}",
headers: {
Authorization: "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiOWVmMDMzYTctMzM3OC00YjkwLWFmZDktN2FmMjdlMTU1OTk3IiwidHlwZSI6ImFwaV90b2tlbiJ9.i79IyhfCGvz8lgvqsZmwbUTNRvqNsAd7kA7F6oDrIv4",
"Content-Type": "multipart/form-data; boundary=" + form.getBoundary(),
},
data: form,
};
axios
.request(options)
.then((response) => {
console.log(response.data);
})
.catch((error) => {
console.error(error);
});
<?php
$url = "https://api.edenai.run/v2/{feature}/{subfeature}";
// Replace "your_API_key" with your actual API key
$headers = [
"Authorization: Bearer your_API_key",
"Content-Type: application/json"
];
// Payload data
$payload = [
"providers" => "<provider/model>",
"text" => "<string value>",
"language" => "<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);
?>
curl -X POST "https://api.edenai.run/v2/{feature}/{subfeature}" \
-H "Authorization: Bearer your_API_key" \
-H "Content-Type: application/json" \
-d '{
"providers": "<provider/model>",
"text": "<string value>",
"language": "<string value>"
}'
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/{feature}/{subfeature}";
string apiKey = "your_API_key";
// Payload
var payload = new
{
providers = "<provider/model>",
text = "<string value>",
language = "<string value>"
};
// Serialize the payload to JSON
string jsonPayload = JsonConvert.SerializeObject(payload);
using (HttpClient client = new HttpClient())
{
// Add Authorization header
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");
// Create StringContent with JSON payload
var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
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}");
}
}
}
}
Synchronous API with file input
import json
import requests
headers = {"Authorization": "Bearer your_API_key"}
url = "https://api.edenai.run/v2/{feature}/{subfeature}"
files = {"doc_1":open('/path/to/your/doc.pdf', 'rb')}
data = {"providers":"<provider/model>"}
response = requests.post(url, files=files, data=data, headers=headers)
result = json.loads(response.text)
const axios = require("axios").default;
const fs = require("fs");
const FormData = require("form-data");
const form = new FormData();
form.append("providers", "<provider/model>");
form.append("file", fs.createReadStream("🖼️ path/to/your/image.png"));
const options = {
method: "POST",
url: "https://api.edenai.run/v2/{feature}/{subfeature}",
headers: {
Authorization: "Bearer your_API_key",
"Content-Type": "multipart/form-data; boundary=" + form.getBoundary(),
},
data: form,
};
axios
.request(options)
.then((response) => {
console.log(response.data);
})
.catch((error) => {
console.error(error);
});
<?php
$url = "https://api.edenai.run/v2/{feature}/{subfeature}";
// 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";
// Data payload
$data = [
"providers" => "<provider/model>"
];
// Initialize cURL
$ch = curl_init();
// Prepare the file data for multipart/form-data
$postFields = array_merge(
$data,
["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/{feature}/{subfeature}" \
-H "Authorization: Bearer your_API_key" \
-F "doc_1=@/path/to/your/doc.pdf" \
-F "providers=<provider/model>"
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/{feature}/{subfeature}";
string apiKey = "your_API_key";
string filePath = "/path/to/your/doc.pdf";
// Data payload
var data = new MultipartFormDataContent
{
{ new StringContent("<provider/model>"), "providers" }
};
// Add the file to the payload
using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
var fileContent = new StreamContent(fileStream);
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/pdf");
data.Add(fileContent, "doc_1", Path.GetFileName(filePath));
}
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, data);
// 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}");
}
}
}
}
Synchronous API with both file and text inputs
import json
import requests
headers = {"Authorization": "Bearer your_API_key"}
url = "https://api.edenai.run/v2/{feature}/{subfeature}"
files = {"doc_1":open('/path/to/your/doc.pdf', 'rb')}
data = {"providers":"<provider/model>","text":"<string value>"}
response = requests.post(url, files=files, data=data, headers=headers)
result = json.loads(response.text)
const axios = require("axios").default;
const fs = require("fs");
const FormData = require("form-data");
const form = new FormData();
form.append("providers", "<provider/model>");
form.append("file", fs.createReadStream("🖼️ path/to/your/image.png"));
form.append("text", "<string value>");
const options = {
method: "POST",
url: "https://api.edenai.run/v2/{feature}/{subfeature}",
headers: {
Authorization: "Bearer your_API_key",
"Content-Type": "multipart/form-data; boundary=" + form.getBoundary(),
},
data: form,
};
axios
.request(options)
.then((response) => {
console.log(response.data);
})
.catch((error) => {
console.error(error);
});
<?php
$url = "https://api.edenai.run/v2/{feature}/{subfeature}";
// 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";
// Data payload
$data = [
"providers" => "<provider/model>",
"text" => "<string value>"
];
// Initialize cURL
$ch = curl_init();
// Prepare the file data for multipart/form-data
$postFields = array_merge(
$data,
["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/{feature}/{subfeature}" \
-H "Authorization: Bearer your_API_key" \
-F "doc_1=@/path/to/your/doc.pdf" \
-F "providers=<provider/model>" \
-F "text=<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/{feature}/{subfeature}";
string apiKey = "your_API_key";
string filePath = "/path/to/your/doc.pdf";
// Create multipart form data
var formData = new MultipartFormDataContent
{
{ new StringContent("<provider/model>"), "providers" },
{ new StringContent("<string value>"), "text" }
};
// Add the file
using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
var fileContent = new StreamContent(fileStream);
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/pdf");
formData.Add(fileContent, "doc_1", Path.GetFileName(filePath));
}
using (HttpClient client = new HttpClient())
{
// Add Authorization header
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");
try
{
// Send POST request
HttpResponseMessage response = await client.PostAsync(url, formData);
// Ensure the response was successful
response.EnsureSuccessStatusCode();
// Read and parse the response
string responseContent = await response.Content.ReadAsStringAsync();
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}");
}
}
}
}
Asynchronous API - Launch request
import json
import requests
headers = {"Authorization": "Bearer your_API_key"}
url = "https://api.edenai.run/v2/{feature}/{subfeature}"
data = { "providers": "provider", "language": "<string value>"}
files = {'file': open("🔊 path/to/your/sound.mp3", 'rb')}
response = requests.post(url, data=data, files=files, headers=headers)
result = json.loads(response.text)
print(result)
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/{feature}/{subfeature}"
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/{feature}/{subfeature}";
// Replace your full token here
$headers = [
"Authorization: Bearer your_API_key"
];
// File path to the sound file
$filePath = "🔊 path/to/your/sound.mp3";
// Data payload
$data = [
"providers" => "provider",
"language" => "<string value>"
];
// Initialize cURL
$ch = curl_init();
// Prepare the file data for multipart/form-data
$postFields = array_merge(
$data,
["file" => 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/{feature}/{subfeature}" \
-H "Authorization: Bearer your_API_key" \
-F "providers=provider" \
-F "language=<string value>" \
-F "file=@🔊 path/to/your/sound.mp3"
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/{feature}/{subfeature}";
string apiKey = "your_API_key";
string filePath = "🔊 path/to/your/sound.mp3";
// Create multipart form data
var formData = new MultipartFormDataContent
{
{ new StringContent("provider"), "providers" },
{ new StringContent("<string value>"), "language" }
};
// Add the file
using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
var fileContent = new StreamContent(fileStream);
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("audio/mpeg");
formData.Add(fileContent, "file", Path.GetFileName(filePath));
}
using (HttpClient client = new HttpClient())
{
// Add Authorization header
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");
try
{
// Send POST request
HttpResponseMessage response = await client.PostAsync(url, formData);
// Ensure the response was successful
response.EnsureSuccessStatusCode();
// Read and parse the response
string responseContent = await response.Content.ReadAsStringAsync();
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}");
}
}
}
}
Asynchronous API - Get result
import json
import requests
headers = {"Authorization": "Bearer your_API_key"}
url = "https://api.edenai.run/v2/{feature}/{subfeature}/{public_id}"
response = requests.get(url, headers=headers)
result = json.loads(response.text)
print(result)
const fetch = require('node-fetch'); // Make sure to install `node-fetch` if running in Node.js
const url = "https://api.edenai.run/v2/{feature}/{subfeature}/{public_id}";
const headers = {
"Authorization": "Bearer your_API_key"
};
// Send the GET request
fetch(url, { method: "GET", headers: headers })
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.json(); // Parse JSON response
})
.then(result => {
console.log(result); // Print the result
})
.catch(error => {
console.error(`Error: ${error.message}`);
});
<?php
$url = "https://api.edenai.run/v2/{feature}/{subfeature}/{public_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/{feature}/{subfeature}/{public_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/{feature}/{subfeature}/{public_id}";
string apiKey = "your_API_key";
using (HttpClient client = new HttpClient())
{
// Add Authorization header
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");
try
{
// Send GET request
HttpResponseMessage response = await client.GetAsync(url);
// Ensure the response was successful
response.EnsureSuccessStatusCode();
// Read and parse the response
string responseContent = await response.Content.ReadAsStringAsync();
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}");
}
}
}
}
Updated 1 day ago