Skip to content

When it comes to "Network Bodhisattva," many people probably know who it refers to—yes, it's Cloudflare. Almost all of their products, including but not limited to CDN, KV database, Pages, and Workers, offer generous free resources and encourage people to use them for free. More importantly, the free quotas are very high. Generally, small businesses or personal studios can support their operations without spending a single penny.

Recently, Cloudflare has launched free AI services. Directly from their website backend, without the need for a server or domain name, you can set up a free, usable AI service in just a few simple steps, including translation, text generation, text-to-image, and speech recognition. This is truly a wave of benefits—don't miss out!

This article mainly introduces how to build a usable free translation API in the Cloudflare backend using the m2m100-1.2B model. The final result is as follows.

This article mainly includes:

  1. Registering an account and logging in
  2. Creating a Worker service
  3. Copying and pasting the code and deploying it
  4. Binding your own domain name (optional)
  5. Using this API in code or software

1. Register an Account and Log In

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

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

2. Create a Worker Service

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

Then click "Create."

On 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 name assigned to you. It can only consist of numbers, English letters, underscores, and hyphens, as shown in the image below. Then save—done.

Next, you'll be told that the deployment was successful, but you still need to paste the code to replace the default one.

3. Copy and Paste the Code

Click "Edit Code" from the last step of the previous step, wait for the page to load, then delete all the code on the left and paste the following code into the left side. The 123456 is your access key. You can modify it to prevent others from using it and wasting your free quota.

// 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 until the "Save and Deploy" button in the top right corner becomes clickable, then click it. In the confirmation window that pops up, confirm again and deploy.

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

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 Name

If you think this address is too long or inconvenient, or if workers.dev might be blocked in your region, making it inaccessible domestically without a proxy, you can bind your own domain name.

First, you need to change your domain name's NS servers to Cloudflare's. Wait for it to take effect, then return to the Cloudflare homepage https://dash.cloudflare.com/ and bind your domain name.

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

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

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

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

Great! Now you can use your own domain name to access it.

5. Use This API in Code and Software

First, remember 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), Fulah (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), Oriya (or), Punjabi (pa), Polish (pl), Pashto (ps), Portuguese (pt), Romanian (ro), Russian (ru), Sindhi (sd), Sinhala (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 Python code to use this API

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 it in video translation software

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

After confirming it works, select "TransAPI" in the translation channel, and you can happily use the free API and free service resources to translate your videos. The free monthly quota is generally sufficient for use.