Skip to content

Many are likely familiar with the internet's benevolent entity, Cloudflare. Indeed, most of their products, including but not limited to CDN, KV databases, Pages, and Workers, offer generous free resources and actively encourage users to leverage them. Crucially, the free tiers are substantial, allowing small businesses or individual studios to sustain their operations without spending a dime.

Recently, Cloudflare launched free AI services directly from their dashboard. With just a few simple steps, you can set up a free and usable AI service without needing a server or domain. These services include translation, text generation, text-to-image, and speech recognition – a truly amazing perk not to be missed!

This article primarily focuses on how to set up a free and functional translation API within the Cloudflare dashboard, utilizing the m2m100-1.2B model. The final result will look like this:

This article covers:

  1. Register and Log In
  2. Create a Worker Service
  3. Copy, Paste Code, and Deploy
  4. Bind Your Own Domain (Optional)
  5. Use This API in Code or Software

1. Register and Log In

If you don't have an account yet, click this link to register. It's very straightforward. https://dash.cloudflare.com/sign-up

If you already have an account, log in directly here: https://dash.cloudflare.com/login

2. Create a Worker Service

After logging in, click on "Workers & Pages" on the left sidebar.

Then click "Create".

In the new page that opens, click "AI" on the right, find "Translation App", hover over it, and click.

On the next screen, set a name. This name will be part of the domain assigned to you and can only contain numbers, English letters, underscores, and hyphens, as shown below. Then save – done!

Next, you'll be informed that the deployment was successful. However, you still need to paste your code to replace the default.

3. Copy, Paste Code, and Deploy

Click "Edit code" from the last step, wait for the page to load, then delete all the code on the left and paste the following code. The 123456 is your access key; you can modify it to prevent others from using up your free usage.

// This is the access key
const SECRET_PASS="123456"

export default {
  async fetch(request, env) {
    const urlStr = request.url
    const urlObj = new URL(urlStr)
    let text =  urlObj.searchParams.get('text')
    let source_language = urlObj.searchParams.get('source_language')
    let target_language = urlObj.searchParams.get('target_language')
    let secret = urlObj.searchParams.get('secret')
    if(secret!==SECRET_PASS){
      return Response.json({code:1,msg:"Unauthorized access",text:text,source_language:source_language,target_lanuage:target_language,secret:secret});
    }
    const inputs = {
      text: text,
      source_lang: source_language,
      target_lang: target_language,
    };
    const response = await env.AI.run('@cf/meta/m2m100-1.2b', inputs);

    if(response.translated_text.indexOf('ERROR')===0){
      return Response.json({code:2,msg:"ok",text:response.translated_text});
    }
    return Response.json({code:0,msg:"ok",text:response.translated_text });

  },
};

After pasting, wait for the "Save and deploy" button in the top right to become clickable, then click it. In the confirmation pop-up, confirm and deploy again.

Great, you can now happily use it! For example, if my deployed address is https://my-translate-api.2124455076.workers.dev/, I can use it directly via this URL:

https://my-translate-api.2124455076.workers.dev/?text=%E4%BD%A0%E5%A5%BD%E5%95%8A%E6%88%91%E7%9A%84%E6%9C%8B%E5%8F%8B&source_language=zh&target_language=en&secret=123456

This address is your translation API endpoint.

4. Bind Your Own Domain (Optional)

If you find this address too long or inconvenient, or if workers.dev might be blocked in certain regions preventing access without a proxy, you can bind your own domain.

First, you need to change your domain's Name Servers (NS) to Cloudflare's. After the changes propagate, return to the Cloudflare homepage https://dash.cloudflare.com/ and bind your domain.

NSezra.ns.cloudflare.com
NSkarsyn.ns.cloudflare.com

After adding, go back to your Worker. Remember how to get there? Click "Workers & Pages" on the left sidebar.

On the right, you'll see your created service. Click its name to enter the settings page.

As shown in the image, first click "Settings --> Triggers", then click "Add Custom Domain" to add your own domain. For instance, if your domain bound to Cloudflare is abc.com, you can set api.abc.com here.

Great, you can now access it using your own domain!

5. Use This API in Code or Software

First, note the language codes supported by m2m100:

Afrikaans (af), Amharic (am), Arabic (ar), Asturian (ast), Azerbaijani (az), Bashkir (ba), Belarusian (be), Bulgarian (bg), Bengali (bn), Breton (br), Bosnian (bs), Catalan (ca), Cebuano (ceb), Czech (cs), Welsh (cy), Danish (da), German (de), Greek (el), English (en), Spanish (es), Estonian (et), Persian (fa), Fula (ff), Finnish (fi), French (fr), Western Frisian (fy), Irish (ga), Scottish Gaelic (gd), Galician (gl), Gujarati (gu), Hausa (ha), Hebrew (he), Hindi (hi), Croatian (hr), Haitian Creole (ht), Hungarian (hu), Armenian (hy), Indonesian (id), Igbo (ig), Ilocano (ilo), Icelandic (is), Italian (it), Japanese (ja), Javanese (jv), Georgian (ka), Kazakh (kk), Central Khmer (km), Kannada (kn), Korean (ko), Luxembourgish (lb), Ganda (lg), Lingala (ln), Lao (lo), Lithuanian (lt), Latvian (lv), Malagasy (mg), Macedonian (mk), Malayalam (ml), Mongolian (mn), Marathi (mr), Malay (ms), Burmese (my), Nepali (ne), Dutch (nl), Norwegian (no), Northern Sotho (ns), Occitan (oc), Odia (or), Punjabi (pa), Polish (pl), Pashto (ps), Portuguese (pt), Romanian (ro), Russian (ru), Sindhi (sd), Sinhalese (si), Slovak (sk), Slovenian (sl), Somali (so), Albanian (sq), Serbian (sr), Swati (ss), Sundanese (su), Swedish (sv), Swahili (sw), Tamil (ta), Thai (th), Tagalog (tl), Tswana (tn), Turkish (tr), Ukrainian (uk), Urdu (ur), Uzbek (uz), Vietnamese (vi), Wolof (wo), Xhosa (xh), Yiddish (yi), Yoruba (yo), Chinese (zh), Zulu (zu)

Using the API with Python code

import requests
response = requests.get(url="https://transapi.pyvideotrans.com/?text=你好啊我的朋友们&source_language=zh&target_language=en&secret=123456")

print(response.json())

It's that simple.

Using in video translation software

Open the settings menu in the top left — "Custom Translation API", fill in your API address and key, then test it.

Once confirmed, select "TransAPI" as the translation channel, and you can happily use the free API and service resources to translate your videos. The free quota is generally sufficient for monthly use.