Developers
Make your first API request
Create a read-only API key, check that it works, and list your products with curl, JavaScript or Ruby.
Reviewed
On this page
This quickstart takes about five minutes. You create a key that can only read, check that it works, and list the products in your store. Nothing in your store changes.
Before you start
- You can sign in to your Crate dashboard as the owner, or as a teammate who can manage settings.
- Your email address is confirmed. Crate won't create API keys until it is.
- You have a terminal with
curl, or Node.js 18 or later, or Ruby.
Step 1: Create a read-only key
- Open Settings, then choose API keys under Developer.
- Click Create API key.
- Enter a Name, such as "First request".
- Under Permissions, choose Read Only.
- Leave Expiration on Never, or choose 30 days for a key you only need today.
- Click Create API key.
alt: The Create new API key window with the name First request and Read Only selected
caption: A Read Only key can look at your store but can't change it.Step 2: Copy the key
The API key created window shows the key once. Click Copy, then Done. Keys start with crate_pat_.
alt: The API key created window with the key blurred and the Copy and Done buttons
caption: Copy the key before you click Done.Save the key in a shell variable so it stays out of your commands:
export CRATE_API_KEY="crate_pat_your_key_here"Step 3: Check that the key works
The account endpoint needs no permission beyond a valid key. It is the quickest way to test one.
curl https://<crate-address>/api/v1/account \
-H "Authorization: Bearer $CRATE_API_KEY"A working key returns your store and the key's details:
{
"account": {
"store": { "id": 12, "name": "Harbour & Hearth", "code": "harbour-hearth", "url": "…", "currency": "USD" },
"token": { "type": "personal_access_token", "name": "First request", "scopes": ["read_products", "read_orders", "…"], "last_used_at": "…" },
"api_version": "v1"
}
}scopes lists what the key may do. A Read Only key holds a read_ scope for every group.
Step 4: List your products
Ask for the first three products. limit sets how many come back.
curl
curl "https://<crate-address>/api/v1/products?limit=3" \
-H "Authorization: Bearer $CRATE_API_KEY"JavaScript
const res = await fetch("https://<crate-address>/api/v1/products?limit=3", {
headers: { Authorization: `Bearer ${process.env.CRATE_API_KEY}` },
});
if (!res.ok) throw new Error(`Crate API ${res.status}: ${await res.text()}`);
const { products } = await res.json();
console.log(products.map((p) => `${p.name} ${p.price} ${p.currency}`));Ruby
require "net/http"
require "json"
uri = URI("https://<crate-address>/api/v1/products?limit=3")
req = Net::HTTP::Get.new(uri, "Authorization" => "Bearer #{ENV.fetch('CRATE_API_KEY')}")
res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) { |http| http.request(req) }
raise "Crate API #{res.code}: #{res.body}" unless res.is_a?(Net::HTTPSuccess)
JSON.parse(res.body)["products"].each { |p| puts "#{p['name']} #{p['price']} #{p['currency']}" }The response is a JSON object with a products list. Each product has fields such as id, name, slug, status, price, currency, sku, stock, collections and images. price is a decimal string when a price is available in the store currency, such as USD. When that price is unavailable, price is JSON null; currency still identifies the expected currency of the store your API key belongs to. A price in another currency is not relabeled as store money.
To get the next page, follow the Link response header. Pagination shows how.
If something goes wrong
-
401 with "Missing bearer token": the
Authorizationheader didn't arrive. Check that it readsBearer, a space, then the key. - 401 with "Invalid or expired access token": the key is wrong, expired or revoked. Create a new one.
- 403 with "Token is missing the 'read_products' scope": the key can't read products. Edit the key and tick Read for Products.
Errors lists every status code.
What's next
Keep the key, or revoke it from its menu on the API keys page if you were only trying things out. For a real integration, create a key with only the permissions it needs.
Before you build anything that runs on its own, read Errors and Rate limits.
Troubleshooting
Next steps
Was this helpful?
Still need a hand?
Tell us the task you were trying to complete and the message you saw. Include your store name or order reference when relevant. Keep passwords, API secrets and card details private.
Contact Crate