figma.importStyleByKeyAsync throwing Error: Unable to create style

This plugin API was working before, after today’s update we got this error

Error: Unable to create style

Any advice?

1 Like

Could you give an example of the code you use to import and create the style?
Also make sure the style with the key you are using exists in the library file.

I’ve run into this issue, too. @Sonny_Lazuardi_Hermawan were you able to get your issue resolved?

I’m having this issue, too. Here’s a code snippet.

async function toStyle(style) {

    let styles = {
        style_1: await figma.importStyleByKeyAsync(styleKey.style1),
    }

    ...

    inst.fillStyleId = styles.style_1.id;

}

Any help you can provide would be much appreciated! Thanks!

Unfortunately, I can’t reproduce the error on my end. That is, my style is successfully imported and applied to the node.

1 Like

Thanks for your attention @tank666 . I swear I had this working just last week…

I’ve done some diligence trying to simplify my attempt to just the essentials to try and get clarity.

I’m storing hard coded style keys in an object at the top level, followed by my async function ‘toStyle’ which takes an argument that’s passed to a switch/case.

Within the async function, I create an ‘itemStyle’ object where I collect the imported the styles, then loop through the selection, and attempt to apply the styles to the selections based on the switch/case argument.

I believe the error is occurring when the function is attempting to import the styles: ‘Uncaught (in promise) Error: Unable to create style’

const styleKey = {
  style_1: [key],
  [...]
}

async function toStyle(arg) {

  let itemStyle = {
    itemStyle_1: await figma.importStyleByKeyAsync(styleKey.style_1),
    [...]
  }
  
  for(const item in figma.currentPage.selection) {
    switch(arg) {
      case 'applyStyle_1': 
        figma.currentPage.selection[item].fillStyleId = itemStyle.itemStyle_1.id; 
        break;
      [...]
    }
  }
}

Interestingly, as I’m opening a fresh Figma file for testing and placing my components to test on, I’m getting some messages in the console that read:

[symbol] failed to repair styles

Thanks again for your attention.

I’m facing same issue with this pluginAPI

 await figma.importStyleByKeyAsync(style.key)

After inspecting, the figma API respond with no error and successfully fetch the style, but the style key returned is different with the one requested. I assume the error happen on the client side.

Most of the styles can load correctly withawait figma.importStyleByKeyAsync(style.key), but only 50 of them in our library getting this error. Those impacted styles are created in 2021.

@Hao_Zhang So importStyleByKeyAsync throws an error for you for some styles, but the network request is succeeding? If you’re hard-coding the style keys some where, we recommend that you migrate to use the new keys returned by the network request.

The background here is that we’ve been making backend changes to the style key contents and so plugins using hard-coded keys obtained a while ago may end up using outdated keys.

@Sonny_Lazuardi_Hermawan Are you seeing the same behavior with the error being thrown but the network request to fetch the style succeeding? If so, I’d recommend using the info above to switch to using the new key from the network response.

hi, i have the same issue

1 Like

i use restful api to get all styles from one lib file.Then i use figma.importStyleByKeyAsync to import all style key. On some file it looks fine.But in some files, this error occurs

request return the same key that i used to importStyleByKeyAsync

1 Like

I see the same error when I run the plugin against a file I have the editor’s access to. It doesn’t happen if I copy the same file and run the same plugin against as an owner.
Does this bug have anything to do with the authority?

I’m using the latest style key return by figma API. Not sure if the backend changes you mentioned affected those style created a few years ago.

The network request was success. I believe it’s the Figma client that throws the error

I’m using https://api.figma.com/v1/files/${file_id} to fetch all the style keys. My guess here it might related to this api returning outdated style keys.

1 Like

i found that style key get from rest api return is different from getLocalPaintsStyles
that’s why this error happen

1 Like

yes, if you run getLocalPaintsStyles in lib file,you will find that key is different from https://api.figma.com/v1/files/${file_id}

1 Like

Hi, any solution for this one? Also ran into the same error

Hi, is there a solution for this issue?

I already confirmed that the style ids where updated (they didn’t change in my case) and it keeps throwing the same error.

Hi all, we plan to roll out a fix for this “Unable to create style” error by the end of day Wednesday March 15. This error primarily comes up for older styles that have been moved to another file.

2 Likes

Still getting the same error. Maybe there is something I need to do with the style in the lib for the fix to work?