Introduction
District has two GraphQL APIs: the Marketplace API for storefronts, and the Admin API for seller tools and sync services. This page gives the GraphQL rules that both APIs follow, a recommended client, and the keys that each kind of app sends.
About GraphQL
Each API has one endpoint. Send each operation to it as an HTTP POST with a JSON body that holds the query and its variables. A query selects only the fields that you need, and the data of the response has the same shape as the selection.
Objects such as products and orders have a global id: give it to node(id:) to read the object again. A field that returns a connection takes first and after: request a page with first, and the next page with after set to pageInfo.endCursor, while pageInfo.hasNextPage is true. When the API refuses a request, for example an invalid query or a request without a valid key, the response has a top-level errors list. A mutation returns an expected failure as a typed result: select ... on Error { message } in its payload.
Use graphql-request to send operations, and gql.tada to type them. gql.tada reads the schema of the API and infers the types of the result and the variables from the text of each query. You do not run a code generator after you change a query.
Add the gql.tada TypeScript plugin to tsconfig.json. The plugin reads the schema from the endpoint and writes it to tadaOutputLocation. To write the file outside your editor, for example in CI, run gql-tada generate-output.
{
"compilerOptions": {
"plugins": [
{
"name": "gql.tada/ts-plugin",
"schema": "https://marketplace.api.district.net/graphql",
"tadaOutputLocation": "./src/graphql-env.d.ts"
}
]
}
}Then write a typed query and send it:
import { initGraphQLTada } from 'gql.tada'
import { GraphQLClient } from 'graphql-request'
import type { introspection } from './graphql-env.d.ts'
const graphql = initGraphQLTada<{ introspection: introspection }>()
const CLIENT_AUTHORIZATION = 'Bearer district_pk_live_…'
const client = new GraphQLClient('https://marketplace.api.district.net/graphql', {
headers: { 'District-Client-Authorization': CLIENT_AUTHORIZATION },
})
const ProductsQuery = graphql(`
query Products($after: String) {
products(first: 10, after: $after) {
edges {
node {
id
title
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
`)
const { products } = await client.request(ProductsQuery, { after: null })For the Admin API, use https://admin.api.district.net/graphql and the headers of your kind of app. Authentication gives the headers.
Introspection
Both APIs answer GraphQL introspection queries. An introspection query needs no key and no access token, so a tool can read the schema before you have a key.
Desktop clients such as Bruno, HTTPie, Insomnia and Postman load the schema from the endpoint. They use it to show the fields that you can select and to complete your queries. To run an operation, add the headers of your kind of app.
Authentication
Each request to a District API sends a client key. The kind of app that you build selects the API, the key and the sign-in.
district_pk_live_…) can go in a browser or an app. Keep every other District key on your server.Choose how you connect
Storefront
- Who uses it?
- Your buyers
- What it is
- A shop or an app for buyers
- API
- Marketplace API
- Key
- Publishable
district_pk_live_… - Sign-in
- OptionalBuyers sign in for bookmarks and similar features.
- Runs in
- browser
- app
- server
Seller tool
- Who uses it?
- Your sellers and staff
- What it is
- A back office, a pick list, a label printer
- API
- Admin API
- Key
- Publishable
district_pk_live_… - Sign-in
- RequiredEach member signs in.
- Runs in
- browser
- app
- server
Sync service
- Who uses it?
- Nobody: it runs on its own
- What it is
- Inventory and order sync from your own systems
- API
- Admin API
- Key
- Restricted
district_rk_live_… - Sign-in
- NoneThe key acts for itself.
- Runs in
- your server only
Storefront
A storefront is a shop or an app for buyers. It uses the Marketplace API.
Send the publishable key in the District-Client-Authorization header of each request. The key is safe to use in a browser or an app. Reach out to your Enterprise account manager to get your key.
District-Client-Authorization: Bearer district_pk_live_…Buyer sign-in is optional. To act for a signed-in buyer, also send the access token of the buyer in the Authorization header. Without it, the API answers for a visitor who is not signed in, and viewer is null.
Authorization: Bearer <buyer access token>Some fields need a client key with a scope. The reference marks each of these fields.
Seller tool
A seller tool is an app for the sellers and staff of a marketplace, such as a back office, a pick list or a label printer. It uses the Admin API.
Each request sends the publishable key and the access token of a signed-in member of the marketplace. Each request needs a member.
District-Client-Authorization: Bearer district_pk_live_…
Authorization: Bearer <member access token>The client of your key needs the admin scopes that your tool uses. Your Enterprise solutions team sets them up.
The role of the member also limits each operation. The reference shows the minimum role of each operation.
Sync service
A sync service moves inventory and orders between your own systems and District. It runs on your server, and no person signs in. It uses the Admin API with a restricted key (district_rk_live_…) that your District contact gives you.
Send only the restricted key, in the District-Client-Authorization header:
District-Client-Authorization: Bearer district_rk_live_…Header to leave out
Do not send an Authorization header. A request with an Authorization header, also an empty or a Basic one, does not run as the agreed member. Some HTTP clients add an empty Authorization header: make sure that yours does not.
What the key can do
The key works for one marketplace only. Each request runs as one member of that marketplace, which you agree with District. The history of a listing or an order shows each change as a change by that member.
The member must stay an active member of the marketplace. If the member is removed, the key stops working. A later release records your app in place of the member. Your requests do not change.
The key has these scopes:
admin:products:readadmin:products:writeadmin:products:deleteadmin:orders:readadmin:orders:settleadmin:shipments:readadmin:shipments:write
The API refuses an operation that needs another scope, for example a payout operation.
Keep the key secret
The key is a server secret. Do not put it in a browser, a mobile app or a public repository.
Rotation
District can give you a new key for the same client, with no other change. When District revokes a key, the key stops working in about 60 seconds.
Sync service keys do not get webhooks yet.
Signing in
Both APIs use District Auth access tokens. Send the token in the Authorization header: Authorization: Bearer <access token>.
- Storefronts: optional. Buyers sign in for features such as bookmarks.
- Seller tools: required. Each member signs in.
- Sync services: no sign-in. The restricted key acts for itself.
Documentation coming soon. Your Enterprise solutions team will reach out to you if your web app needs it.
Continue to the guides
Marketplace API
Build a storefront for one marketplace: products, carts, checkout and orders.
Admin API
Run a marketplace: listings, orders, shipping and members.
Webhooks
Get an HTTPS request when a listing, an order, a livestream or a seller changes.