Unable to import component by key

Hello, I created a plugin last year which largely deals with selecting a component variant in the plugin UI and then the plugin will automatically import the corresponding variant of a published component. However, when I went to update the plugin, it no longer works. The console is giving me the error message: Uncaught (in promise) Could not find a published component set with the key “(key)”.

I tried troubleshooting this by going into the console of a component within Figma. The component is published. I selected a variant and typed in: “Figma.currentPage.selection[0].key”. I then copied this key and typed in "Figma.importComponentByKeyAsync(‘key’) with replacing “key” with the actual key. Again I get the error: “Uncaught (in promise) Could not find a published component set with the key “(key)”.”

I am dumbfounded why this is happening. I have tried multiple component variants. Anyone have any idea how to fix this?

Is the component you are trying to import in the same or different file where you are importing it? Is the file where you are importing it in the same team as the component?

The components are published to the entire team. I tried importing an instance through the console in the same file the component was created in and that doesnt work. However i can easily import the component instance using Figma’s UI. Its like it doesnt recognize the key for some reason.

This shouldn’t actually be possible, I believe you can only import instances in external files. In the local file you can get the main component directly (instance.mainComponent).

Can you check that you don’t have any extra symbols in the key by accident, e.g. brackets? The exact error I’m getting when running figma.importComponentByKeyAsync("1234") is:

Uncaught (in promise) Could not find a published component with the key "1234"

You wrote:

Uncaught (in promise) Could not find a published component set with the key "(key)"

Does your key also contain brackets or did you add them here on the forum?

Please use the ` symbol to wrap code when writing code here on the forum (or </> button in the toolbar). This way all symbols appear as is in code without automatic replacements so it’s easier to debug your code.

By the way, are you sure you are selecting the component and not a component set (a container with variants)? That one uses a different import function.

Thanks Gleb. I think the issue was doing the import within the same file as the component. I appreciate your help!