Re: Access admin API from within theme code?

Can admin API be accessed directly within theme code?

Ivo37
Tourist
4 0 1

Hello all, 

The question may be due to lack of understanding of the shopify APIs and App vs theme system, please excuse that. I am extending a theme (Pixel Unions Vogue), adding functionality and customizing, for several weeks now, and have developed understanding of liquid, html, css, and Javascript, and how the theme code works. (I am a computer engineer, but without previous experience in web development.)

One thing I am currently wondering about and about which I honestly don't find answers by my searches, is:

Can I access the Admin API directly from within the theme code without writing some kind of "external" app l? For example, to get and set information about whether a customer has accepted marketing emails.

Thank you for reading and answering! 

Ivo 

Replies 6 (6)

jchristie
Excursionist
13 2 4

Hey @Ivo37,

It’s a good question. You can’t access the Admin API directly from liquid files, like you suggest, this would require an app with API keys. 

However, as you’ve noticed, some objects accessible through the Admin API are also populated as liquid objects in the liquid files. Luckily, customer is one of these. See the link below for all attributes accessible on the customer object. To answer your specific question though, you can get the Boolean of whether a customer accepts marketing with the following liquid statement:

{{ customer.accepts_marketing }} 

Link to customer liquid object: https://shopify.dev/docs/themes/liquid/reference/objects/customer

Hope this helped! 
James

Ivo37
Tourist
4 0 1

Hi James,

thank you for your response and sorry my reaction is quite late.

The problem in this specific case would be that I can _get_ the Boolean about marketing acceptance, but not _set_ it, if I'm not mistaken.

If I have API keys, is it also not possible to put requests to the API via javascript from the store frontend? Is it not possible, or is it possible, but abuse? 😉 I know, I need to get into the basics of shopify app programming. So far, it seems overly complicated. I only want to program for our own store, creating an "app" seems overkill...

Cheers and thanks,

Ivo

 

sampsonpete
Shopify Partner
3 0 0

Hi Ivo,

I was just looking into how to do something similar. I’d like to save new metafields to a customer and creating an app for it feels like overfill. I agree there’s no documentation or tutorials on this sort of thing out there.

Thinking on it, though, I reckon it may be possible to update the Admin API via a JS request from the theme, but it would be insecure. You’d be posting your API access key for anyone to find and use so I imagine that it’s not recommended.

In terms of your specific need to update the customer marketing preferences, I wonder if this is possible inside the theme by using a HTML form? Have a look at Shopify’s contact or customer forms to see if they do the job.

Best,

Pete

paco41
Visitor
2 0 1

Hello Sampsonpete,

 

Do you have a solution to save metafields from theme?

 

Best regards, Paco

paco41
Visitor
2 0 1

To whom as the same question :

Use app proxy to connect front - back - metafields

SachiA
Shopify Partner
1 0 1

@paco41 Could you explain how to use app proxy to use Admin Api from theme? I'm trying to update metafeilds from theme