GraphQL Admin和REST Admin API入门
GraphQL Admin和REST Admin API使您可以使用GraphQL或REST为Shopify管理员构建应用程序和其他集成。借助这些API,您可以创建在商店运营的每个阶段都提供功能的应用程序,包括运输,履行和产品管理。
验证
GraphQL Admin和REST Admin API需要Shopify访问令牌(对于公共应用程序和自定义应用程序)或API密码(对于私有应用程序)才能发出经过身份验证的请求。
您可以按照OAuth授权流程或通过创建私有应用并使用该应用的API密码来获取访问令牌。
使用OAuth进行身份验证
要获取访问令牌,请遵循OAuth指南中的OAuth授权流程。X-Shopify-Access-Token在请求中包括访问令牌作为标题。
使用基本的HTTP身份验证进行身份验证
- 在Shopify管理员中,点击应用。
- 点击管理私人应用。
- 点击创建新的私人应用。
- 输入您的私人应用程序的详细信息。
- 点击保存。
- 使用生成的API密码作为访问令牌。
GraphQL Admin API
GraphQL Admin API是基于GraphQL的基于REST的Admin API的替代,并使Shopify管理员的功能可在单个GraphQL端点上使用:
POST [https://{shop}.myshopify.com/admin/api/2021-01/graphql.json](https://%7Bshop%7D.myshopify.com/admin/api/2021-01/graphql.json)
您可以使用GraphiQL应用程序,curl或任何HTTP客户端访问GraphQL Admin API:
注意:
Shopify的GraphQL API仅接受POST请求。其他HTTP方法(例如GET或PUT)将返回400(错误请求)或406(不可接受)响应。
使用GraphiQL应用
我们建议安装Shopify自己的GraphiQL应用程序,以使用GraphQL Admin API探索您商店的数据。安装应用程序后,可以通过运行以下查询来对其进行测试:
POST [https://{shop}.myshopify.com/admin/api/2021-01/graphql.json](https://%7Bshop%7D.myshopify.com/admin/api/2021-01/graphql.json)
query {shop {nameprimaryDomain {urlhost}}}
JSON响应:
{"data": {"shop": {"name": "graphql","primaryDomain": {"url": "https://graphql.myshopify.com","host": "graphql.myshopify.com"}}}}
使用卷曲
以下示例显示了对前5个产品ID和句柄的查询。替换{shop}为商店的域和{password}“身份验证”部分中生成的访问令牌。
注意:
如果您使用的是HTTP客户端(例如Postman或Insomnia),则必须将设置Content-Type为application/json而不是application/graphql。
POST [https://{shop}.myshopify.com/admin/api/2021-01/graphql.json](https://%7Bshop%7D.myshopify.com/admin/api/2021-01/graphql.json)
curl -X POST \https://{shop}.myshopify.com/admin/api/2021-01/graphql.json \-H 'Content-Type: application/graphql' \-H 'X-Shopify-Access-Token: {password}' \-d '{products(first: 5) {edges {node {idhandle}}pageInfo {hasNextPage}}}'
JSON响应:
{"data": {"products": {"edges": [{"node": {"id": "gid://shopify/Product/1974208299030","handle": "rough-snowflake-camisole"}}],"pageInfo": {"hasNextPage": true}}}}
查询示例
在GraphQL中,查询等同于REST的GET操作动词。它们通常从下面列出的对象之一开始,[QueryRoot](https://shopify.dev/docs/admin-api/graphql/reference/common-objects/queryroot)并且可以从该对象具有的任何连接中获取数据。即使将POST发送到GraphQL端点,如果主体仅包含查询,则将仅检索数据,而不修改数据。
以下示例显示查询某个位置可用库存商品数量的方法:
POST [https://{shop}.myshopify.com/admin/api/2021-01/graphql.json](https://%7Bshop%7D.myshopify.com/admin/api/2021-01/graphql.json)
{inventoryItem(id: "gid://shopify/InventoryItem/19848949301270") {idinventoryLevels(first: 10) {edges {node {available}}}}}
JSON响应:
{"data": {"inventoryItem": {"id": "gid://shopify/InventoryItem/19848949301270","inventoryLevels": {"edges": [{"node": {"available": 5}}]}}}}
变异示例
变异等效于REST的数据修改动作动词,例如PUT或DELETE。以下示例显示了一种变异,该变异增加了某个位置的可用库存:
POST [https://{shop}.myshopify.com/admin/api/2021-01/graphql.json](https://%7Bshop%7D.myshopify.com/admin/api/2021-01/graphql.json)
mutation {inventoryAdjustQuantity(input:{inventoryLevelId: "gid://shopify/InventoryLevel/13570506808?inventory_item_id=10820777115690"availableDelta: 1}){inventoryLevel {available}}}
JSON响应:
{"inventoryAdjustQuantity": {"inventoryLevel": {"available": 9},"userErrors": []}}
REST管理员API
您可以使用curl或任何其他HTTP客户端访问REST Admin API。REST Admin API端点按资源组织。您需要根据应用程序提供的服务使用不同的API终结点。
GET使用curl的示例请求
以下卷曲请求通过使用Shop资源和GET /admin/api/2021-01/shop.json端点来检索信息。替换{shop}为商店的域和{password}“身份验证”部分中生成的访问令牌。
要求:
复制
curl -X GET \https://{shop}.myshopify.com/admin/api/2021-01/shop.json \-H 'Content-Type: application/json' \-H 'X-Shopify-Access-Token: {password}'
JSON响应:
{"shop": {"id": 690933842,"name": "Apple Computers","email": "steve@apple.com","domain": "shop.apple.com","province": "California","country": "US","address1": "1 Infinite Loop","zip": "95014","city": "Cupertino","source": null,"phone": "1231231234","latitude": 45.45,"longitude": -75.43,"primary_locale": "en","address2": "Suite 100","created_at": "2007-12-31T19:00:00-05:00","updated_at": "2021-01-01T14:39:35-05:00","country_code": "US","country_name": "United States","currency": "USD","customer_email": "customers@apple.com","timezone": "(GMT-05:00) Eastern Time (US & Canada)","iana_timezone": "America/New_York","shop_owner": "Steve Jobs","money_format": "$","money_with_currency_format": "$ USD","weight_unit": "lb","province_code": "CA","taxes_included": null,"auto_configure_tax_inclusivity": null,"tax_shipping": null,"county_taxes": true,"plan_display_name": "Shopify Plus","plan_name": "enterprise","has_discounts": true,"has_gift_cards": true,"myshopify_domain": "apple.myshopify.com","google_apps_domain": null,"google_apps_login_enabled": null,"money_in_emails_format": "$","money_with_currency_in_emails_format": "$ USD","eligible_for_payments": true,"requires_extra_payments_agreement": false,"password_enabled": false,"has_storefront": true,"eligible_for_card_reader_giveaway": false,"finances": true,"primary_location_id": 905684977,"cookie_consent_level": "implicit","visitor_tracking_consent_preference": "allow_all","force_ssl": true,"checkout_api_supported": true,"multi_location_enabled": false,"setup_required": false,"pre_launch_enabled": false,"enabled_presentment_currencies": ["USD"]}}
范例POST要求
以下示例说明了如何通过使用产品资源和POST /admin/api/2021-01/products.json端点来创建具有草稿状态的产品。
要求:
POST [https://{shop}.myshopify.com/admin/api/2021-01/products.json](https://%7Bshop%7D.myshopify.com/admin/api/2021-01/products.json)
复制
{"product": {"title": "Burton Custom Freestyle 151","body_html": "<strong>Good snowboard!</strong>","vendor": "Burton","product_type": "Snowboard","status": "draft"}}
JSON响应:
{"product": {"id": 4,"title": "Burton Custom Freestyle 151","body_html": "<strong>Good snowboard!<\/strong>","vendor": "Burton","product_type": "Snowboard","created_at": "2020-09-09T17:03:05-07:00","handle": "burton-custom-freestyle-151","updated_at": "2020-09-09T17:03:06-07:00","published_at": "2020-09-09T17:03:05-07:00","template_suffix": null,"status": "draft","published_scope": "web","tags": "","admin_graphql_api_id": "gid:\/\/shopify\/Product\/4","variants": [{"id": 5,"product_id": 4,"title": "Default Title","price": "0.00","sku": "","position": 1,"inventory_policy": "deny","compare_at_price": null,"fulfillment_service": "manual","inventory_management": null,"option1": "Default Title","option2": null,"option3": null,"created_at": "2020-09-09T17:03:06-07:00","updated_at": "2020-09-09T17:03:06-07:00","taxable": true,"barcode": null,"grams": 0,"image_id": null,"weight": 0.0,"weight_unit": "kg","inventory_item_id": 789,"inventory_quantity": 0,"old_inventory_quantity": 0,"requires_shipping": true,"admin_graphql_api_id": "gid:\/\/shopify\/ProductVariant\/6"}],"options": [{"id": 456,"product_id": 4,"name": "Title","position": 1,"values": ["Default Title"]}],"images": [],"image": null}}
范例PUT要求
以下示例说明了如何通过使用“客户地址”资源和PUT /admin/api/2021-01/customers{customer_id}/addresses/{address_id}.json端点来更新客户地址的邮政编码。
要求:
PUT /admin/api/2021-01/customers/207119551/addresses/207119551.json
复制
{"address": {"id": 207119551,"zip": "90210"}}
JSON响应:
{"customer_address": {"id": 207119551,"customer_id": 207119551,"first_name": null,"last_name": null,"company": null,"address1": "Chestnut Street 92","address2": "","city": "Louisville","province": "Kentucky","country": "United States","zip": "90210","phone": "555-625-1199","name": "","province_code": "KY","country_code": "US","country_name": "United States","default": true}}
范例DELETE要求
以下示例说明了如何通过使用订单资源和DELETE /admin/api/2021-01/orders/{order_id}.json端点删除订单。
要求:
DELETE /admin/api/2021-01/orders/450789469.json
JSON响应:
HTTP/1.1 200 OK{}
下一步
- GraphQL查询:了解如何编写更详细的查询以及如何跨多个资源获取数据。
- Shopify Admin API GraphiQL Explorer:使用GraphiQL Explorer构建GraphQL查询。
- GraphQL学习资源:了解有关GraphQL以及如何在您的应用程序中使用它的更多信息。
- REST Admin API参考:探索您可以使用REST Admin API访问的不同资源和端点。
