API Examples

Practical examples of using the Terry Translation API in different scenarios.

Node.js Examples

Basic Translation

const axios = require('axios')

async function translate(text, target) {
  const response = await axios.post(
    'https://api.terry.ai/v1/translate',
    {
      text,
      source: 'en',
      target,
    },
    {
      headers: {
        Authorization: `Bearer ${process.env.TERRY_API_KEY}`,
      },
    },
  )

  return response.data.translation
}

File Translation

const FormData = require('form-data')
const fs = require('fs')

async function translateFile(filePath, target) {
  const form = new FormData()
  form.append('file', fs.createReadStream(filePath))
  form.append('target', target)

  const response = await axios.post(
    'https://api.terry.ai/v1/translate/file',
    form,
    {
      headers: {
        ...form.getHeaders(),
        Authorization: `Bearer ${process.env.TERRY_API_KEY}`,
      },
    },
  )

  return response.data
}

Python Examples

Basic Translation

import requests

def translate(text, target):
    response = requests.post(
        'https://api.terry.ai/v1/translate',
        headers={'Authorization': f'Bearer {api_key}'},
        json={
            'text': text,
            'source': 'en',
            'target': target
        }
    )
    return response.json()['translation']

Batch Translation

def translate_batch(texts, target):
    response = requests.post(
        'https://api.terry.ai/v1/translate/batch',
        headers={'Authorization': f'Bearer {api_key}'},
        json={
            'texts': texts,
            'source': 'en',
            'target': target
        }
    )
    return response.json()['translations']

Next Steps