Excel move VBA WinHttpRequest to Script Fetch fails Failed to fetch at self.fetch
04:42 10 Dec 2025

We use an API call to a custom site run by another company. The calls require a token to be generated first and passed along. The following code to generate a token works with no problem in VBA:

Dim HttpReq As New WinHttp.WinHttpRequest
Dim URL As String
Dim URLPW As String
Dim CURLPW() As Byte
Dim jsonResponse
Dim WS As Worksheet
Dim Token As String

'GET TOKEN
Set WS = ThisWorkbook.Worksheets("Setup")
URL = WS.Range("TokenURL").Value

URLPW = WS.Range("BBody").Value
CURLPW = TextToBinary(URLPW)

For TLen = 0 To UBound(CURLPW)
    WS.Cells(TLen + 8, 1).Value = CURLPW(TLen)
Next

HttpReq.Open "POST", URL, False
HttpReq.SetRequestHeader Header:="Content-Type", Value:="application/x-www-form-urlencoded"
HttpReq.SetRequestHeader Header:="Accept", Value:="application/json"
HttpReq.Send CURLPW

If HttpReq.Status = 200 Then
    jsonResponse = HttpReq.ResponseText
    TLen = InStr(17, jsonResponse, """,", vbTextCompare)
    Token = Mid(jsonResponse, 18, TLen - 18)
    WS.Range("Token").Value = Token
Else
    MsgBox "Error retrieving data. Status code: " & HttpReq.Status, vbCritical
    End
End If
Public Function TextToBinary(ByVal stext As String) As Byte()
    Dim oStream As Object
    Set oStream = CreateObject("ADODB.Stream")
        With oStream
            .Type = 2 ' adTypeText
            .Mode = 3 ' adModeReadWrite
            .Charset = "UTF-8"
            .Open
            .WriteText stext
            .Position = 0
            .Type = 1 ' adTypeBinary
            TextToBinary = .Read
            .Close
        End With
        Set oStream = Nothing
End Function

I was trying to get rid of the VBA and use the newer ExcelScript. But I keep getting the same error

message: "Failed to fetch", name: "TypeError", stack: "TypeError: Failed to fetch at self.fetch (blob:null

async function main(workbook: ExcelScript.Workbook) {
    let Dsheet = workbook.getWorksheet("Setup");
    let OSheetN = Dsheet.getRange("F2").getValue();
    let OSheet = workbook.getWorksheet(OSheetN);

    let TokenUrl: string[] = Dsheet.getRange("TokenURL").getValue();
    let URLPW = Dsheet.getRange("BBody").getValues();
    
    const encoder = new TextEncoder    
    let CURLPW = encoder.encode(URLPW);
    console.log("CURLPW:",CURLPW)

    let FBody = [239,187,191]
    //THE ADODB stream added the UTF8 values to the array so I did the same
    
    CURLPW.forEach(row => FBody.push(row));
    //Combine the UTF8 and the encoded CURLPW together
    
    console.log("FBody:",FBody)
    //Matches [239, 187, 191, 103, 114, 97...]

    try {
        const response = await fetch(TokenUrl, {
            method: "POST", 
            headers: {
                "Content-Type": "application/x-www-form-urlencoded",
                "Accept": "application/json", 
                "Access-Control-Allow-Origin": "*",
            },
            body: FBody
        });

        console.log("Await", response.status)

        // Check if the request was successful
        if (!response.ok) {
            throw new Error(`HTTP error! status: ${response.status}`);
        }

        // Parse the JSON response
        const repos : TokenPull[] = await response.json();
        console.log(repos.values)
    
    } catch (error) {
        console.log("Error fetching or processing API data:", error );
    }
    

    interface TokenPull{
    }
}

I am brand new to Office Scripts!

excel vba office-scripts