Using CF Worker for LINE Webhook and get data from RestAPI

I’m using Workers endpoint URL to set in LINE webhook. When user send ‘text’ in LINE chat it will send shipping track information back to user with Rest API of ThailandPost.

I deployed this code and test for many times until it worked. I recieved data from API but I can be used a few times It can’t anymore.

Important In my CF account I have a same code in full projects but it can recieved data about 8 in 10 (cancelled status sometime) but other projects with same code it not work.

I try to use all code and create new workers it not work the same. So that made me very confused about the problem.

Example code below I just want to get the data from API to show in CF Logs first.

//index.js
const { getItems } = require('./thailandpost.js');

export default {
    async fetch(request, env) {
        const bodyText = await request.text();
        console.log(bodyText)
        const bodyJSON = JSON.parse(bodyText);
        const event = bodyJSON.events[0];

        if (event.type === "message" && event.message.type === "text") {
            const barCode = event.message.text;
            console.log(barCode)
            const trackResult = await getItems(barCode);
            console.log(trackResult);
            return new Response({ status: 200 });
        }
        return new Response('Hello Cloudflare Worker!');
    }
}
//thailandpost.js
const THAILAND_POST_GETTOKEN = 'https://trackapi.thailandpost.co.th/post/api/v1/authenticate/token'
const THAILAND_POST_GETITEMS = 'https://trackapi.thailandpost.co.th/post/api/v1/track'
const ACCESS_TOKEN = 'MYTOKEN'

	async function getToken() {
		const headers = {
			Authorization: `Token ${ACCESS_TOKEN}`,
			"Content-Type": "application/json"
		}
		const options = {
			method: "POST",
			headers
		}
		const response = await fetch(THAILAND_POST_GETTOKEN, options)
		const result = await response.json()
		return result
	}

	async function getItems(barCode) {
		const authToken = await getToken()
		const data = {
			status: "all",
			language: "TH",
			barcode: [barCode]
		}
		const headers = {
			Authorization: `Token ${authToken.token}`,
			"Content-Type": "application/json"
		}
		const options = {
			method: "POST",
			headers,
			body: JSON.stringify(data)
		}
		const response = await fetch(THAILAND_POST_GETITEMS, options)
		const result = await response.json()
		return result
	}

module.exports = { getItems };

I’m trying to recheck API key and others in test.js with command node src/test.js
It completed and received data in terminal 100% work
but in CF workers still can’t.

//test.js
const { getItems } = require('./thailandpost.js');

async function testThailandPostAPI() {
        const barCode = "EM352846050TH";
        const trackResult = await getItems(barCode);
        console.log('Track Result:', (trackResult));
}
testThailandPostAPI();
//Terminal
PS C:\Users\AAAAA\Desktop\post-tracking> node src/test.js
Track Result: {
  response: {
    items: { EM35XXXX050TH: [Array] },
    track_count: {
      track_date: '28/03/2567',
      count_number: 1,
      track_count_limit: 1000
    }
  },
  message: 'successful',
  status: true
}

Other information

Workers and Pages: Paid Plans
Placement Smart
[email protected]

Command

$ npm create cloudflare
$ npm run deploy

I can test by localhost but when I deploy it to CF workers. It 's not work.