JavaScript/Node.js SDK Examples
Code examples for integrating vfrog.ai with JavaScript and Node.js applications.
Installation
npm install axios
Basic Usage
const axios = require('axios')
const API_KEY = process.env.VFROG_API_KEY
const BASE_URL = 'https://api.vfrog.ai/v1/cv'
class VfrogClient {
constructor(apiKey) {
this.apiKey = apiKey
this.headers = {
'x-api-key': apiKey,
'Content-Type': 'application/json',
}
}
async processSync(imageUrl, externalId) {
const response = await axios.post(
`${BASE_URL}/sync_request`,
{
external_id: externalId,
image_url: imageUrl,
},
{
headers: this.headers,
timeout: 30000,
}
)
return response.data
}
}
// Usage
const client = new VfrogClient(API_KEY)
// Sync
client
.processSync('https://example.com/product.jpg', 'scan-456')
.then((result) => {
console.log(`Found ${result.results.length} objects`)
})
React Hook
import { useState, useCallback } from 'react'
import axios from 'axios'
export function useVfrog() {
const [loading, setLoading] = useState(false)
const [error, setError] = useState(null)
const processImage = useCallback(async (imageUrl, externalId) => {
setLoading(true)
setError(null)
try {
const response = await axios.post(
process.env.REACT_APP_VFROG_SYNC_URL,
{
external_id: externalId,
image_url: imageUrl,
},
{
headers: {
'x-api-key': process.env.REACT_APP_VFROG_API_KEY,
'Content-Type': 'application/json',
},
timeout: 30000,
}
)
return response.data
} catch (err) {
setError(err.message)
throw err
} finally {
setLoading(false)
}
}, [])
return { processImage, loading, error }
}
More Examples
See the GitHub repository for more examples.