Error making a POST request in a create-figma-plugin codebase

Hello,
I am working on this Figma plugin and I am using create-figma-plugin toolkit. I want to integrate the plugin with LemonSqueezy’s license key API. I am making a post request and it doesn’t work giving this error:

Error: in fetch: Expected "init.body" to have type Uint8Array but got string instead
    at Proxy.fetch (eval at <anonymous> (eval at createScopedEvaluatorFactory (671d607b-e167-4ac8-a492-12b74d724c97:1:6906)), <anonymous>:23:21)
    at doit (PLUGIN_1_SOURCE:114:29)
    at eval (PLUGIN_1_SOURCE:151:7)
    at eval (PLUGIN_1_SOURCE:56:7)
    at invokeEventHandler (PLUGIN_1_SOURCE:62:35)
    at figma.ui.onmessage (PLUGIN_1_SOURCE:73:11)
    at ptr.callFunction (figma_app.min.js.br:717:4755)
    at iframeMessageHandler (figma_app.min.js.br:1440:17341)
    at EIo.toplevelWindowMessageChannel.port2.onmessage (figma_app.min.js.br:1409:5506)

Here is the code:

try {
      const res = await fetch(
        'https://api.lemonsqueezy.com/v1/licenses/activate',
        {
          method: 'POST',
          headers: {
            Accept: 'application/json',
            'Content-Type': 'application/x-www-form-urlencoded',
          },
          body: 'license_key=38b1460a-5104-4067-a91d-77b872934d51&instance_name=Test',
        }
      )

      const data = await res.json()
      console.log(data)
    } catch (error) {
      console.log(error)
    }

The same code works in codesandbox and in a code base using vanilla js.

Any help is appreciated, thanks

You might need to convert your “body” to a Unit8Array using TextEncoder

See this stackoverflow answer for an example: javascript - Converting between strings and ArrayBuffers - Stack Overflow

Also, I hope that’s not your actual API key, if it is, you might want to regenerate it😅

1 Like

Thanks for the reply, been there it didn’t work. I have since abandoned the feature :frowning: And not that’s not my key, it’s from the doc :slight_smile:

Not sure about create-figma-plugin specifically, but in general I would recommend you do the fetch request inside of the plugin UI instead of plugin backend. The built-in fetch in the plugin backend is not a full-featured replication of the actual fetch. It still has some sandboxed limitations.

1 Like