Skip to main content

Start building for B2B on Shopify

Plus

Only stores on the Shopify Plus plan can use apps with B2B features.

When a merchant engages in business-to-business (B2B) transactions, they sell their goods and services directly to other companies. You can use the GraphQL Admin API to build apps that solve the challenges that come with B2B commerce.


In this tutorial, you'll learn how to do the following tasks:



Anchor to Step 1: Create a companyStep 1: Create a company

When a merchant sells with B2B, a company represents information about who makes a B2B purchase. The following example creates a company by calling the companyCreate mutation with the following inputs:

  • name: The name of the company.
  • companyLocation: The location of the company. A companyLocation must be added to attach important information, like tax exemptions, to the company.
  • companyContact: A contact at the company who can make purchases. A companyContact must be added to create orders and draft orders.

POST https://{shop}.myshopify.com/api/{api_version}/graphql.json

GraphQL mutation

mutation {
companyCreate(input: {
company: {
name: "Example Company"
},
companyLocation: {
name: "Example Company Location"
},
companyContact: {
firstName: "Bo",
lastName: "Wang",
email: "bo.wang@example.com"
}
}) {
company {
id
name
locations(first:10) {
edges {
node {
id
name
}
}
}
contacts(first:10) {
edges {
node {
id
customer {
firstName
lastName
email
}
}
}
}
}
userErrors {
field
message
}
}
}

JSON response

{
"data": {
"companyCreate": {
"company": {
"id": "gid://shopify/Company/1",
"name": "Example Company",
"locations": {
"edges": [
{
"node": {
"id": "gid://shopify/CompanyLocation/1",
"name": "Example Company Location"
}
}
]
},
"contacts": {
"edges": [
{
"node": {
"id": "gid://shopify/CompanyContact/1",
"customer": {
"firstName": "Bo",
"lastName": "Wang",
"email": "bo.wang@example.com"
}
}
}
]
}
},
"userErrors": []
}
}
}

Anchor to Step 2: Create a B2B catalogStep 2: Create a B2B catalog

A catalog is a set of products that's published and priced for certain customers based on specified conditions, such as whether they're buying for a certain B2B company location. For example, you can create a catalog that allows the Canadian branch of a company to purchase only the t-shirt products on the store, and they receive a 30% discount.

You can create and associate a catalog with a company location by passing the company location ID in the context field of the catalogCreate or catalogUpdate mutation.

Note

Multiple catalogs can be associated with the same company location. If a customer is logged into a B2B customer account that's eligible for multiple catalogs that contain the same product, then they receive the lowest listed price within those catalogs.

The following example uses the catalogCreate mutation to create and associate a catalog with the company location from the previous step:

POST https://{shop}.myshopify.com/api/{api_version}/graphql.json

GraphQL mutation

mutation catalogCreate($input: CatalogCreateInput!) {
catalogCreate(input: $input) {
catalog {
title
status
priceList { id }
publication { id }
}
userErrors {
field
code
message
}
}
}

Variables

{
"input": {
"title": "Catalog for testCompany's Canadian branch",
"status": "ACTIVE",
"context": {
"companyLocationIds": ["gid://shopify/CompanyLocation/1"]
},
"priceListId": "gid://shopify/PriceList/1",
"publicationId": "gid://shopify/Publication/1"
}
}

JSON response

{
"data": {
"catalogCreate": {
"catalog": {
"id": "gid://shopify/CompanyLocationCatalog/CREATED-CATALOG-ID",
"title": "Catalog for testCompany's Canadian branch",
"status": "ACTIVE",
"priceList": {
"id": "gid://shopify/PriceList/1"
},
"publication": {
"id": "gid://shopify/Publication/1"
}
},
"userErrors": []
}
}
}
Tip

You can use the PriceList and Publication objects to define a subset of products and prices to be published to this catalog. Learn how catalogs work, and how to associate a price list and publication to a B2B catalog.


Anchor to Step 3: Create a draft order for a companyStep 3: Create a draft order for a company

Merchants might want to create draft orders for company approval when selling B2B.

You can pass the purchasingCompany input to the draftOrderCreate mutation to create a new draft order for a company.

The purchasingCompany input specifies the company ID, location, and contact. Billing and shipping information are included in the response because they aren't automatically populated from the company location. The lineItems object indicates the items to be purchased in the order.

For more information on all possible inputs, refer to the DraftOrders documentation.

POST https://{shop}.myshopify.com/api/{api_version}/graphql.json

GraphQL mutation

mutation{
draftOrderCreate(
input: {
purchasingEntity: {
purchasingCompany: {
companyId: "gid://shopify/Company/1"
companyLocationId: "gid://shopify/CompanyLocation/1"
companyContactId: "gid://shopify/CompanyContact/1"
}
}
billingAddress: {
address1: "273 Lenore Stravenue",
address2: "Suite 573",
city: "Laketown",
company: "Alfredo Group",
firstName: "Avery",
lastName: "Brown",
phone: "(800) 555 0100",
provinceCode: "TN",
zip: "38103"
},
customAttributes: null,
lineItems: [
{
appliedDiscount: null,
originalUnitPrice: "132.99",
quantity: 1,
sku: "",
title: "Aerodynamic Bronze Car",
variantId: "gid://shopify/ProductVariant/349",
taxable: true,
requiresShipping: true,
customAttributes: [],
}
],
note: null,
shippingAddress: {
address1: "273 Lenore Stravenue",
address2: "Suite 573",
city: "Laketown",
company: "Alfredo Group",
firstName: "Avery",
lastName: "Brown",
phone: "(800) 555 0100",
provinceCode: "TN",
zip: "38103"
},
tags: [],
email: "averybrown@example.com",
phone: null,
taxExempt: false,
reserveInventoryUntil: null,
appliedDiscount: null,
shippingLine: null,
localizationExtensions: []
}
) {
draftOrder{
id
}
}
}

JSON response

{
"data": {
"draftOrderCreate": {
"draftOrder": {
"id": "gid://shopify/DraftOrder/10",
},
},
}
}


Was this page helpful?