# Getting started with the Pyck GraphQL API

Audience: Developer

Getting Started with the GraphQL API

## Introduction

This guide shows how to make your first query and understand the response format.

## Prerequisites

Before you begin, you'll need:

- A Pyck account with API access
- Authentication credentials (JWT token or PAT)
- A GraphQL client or HTTP client

## GraphQL endpoint

Pyck has two environments for customers, each with its own GraphQL endpoint:

- `https://eu.pyck.cloud/graphql`: the production environment.
- `https://demo.pyck.cloud/graphql`: the demo environment.

The examples in this guide use the production endpoint. To use the demo environment, replace the host in the examples. Each environment also names its GraphQL endpoint in `endpoints.graphql.url` of its discovery document at `/.well-known/pyck-configuration`, see [Client discovery document](https://docs.pyck.cloud/dev/developers/authentication.md#client-discovery-document).

## Making your first query

This query returns the current user:

```graphql
query {
  me {
    UserID
    Username
    Email
  }
}
```

This query requests the user ID, username, and email of the authenticated user.

### Using curl

```bash
curl -X POST \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  --data '{"query": "query { me { UserID Username Email } }"}' \
  https://eu.pyck.cloud/graphql
```

### Using JavaScript

```javascript
async function fetchCurrentUser() {
  const response = await fetch('https://eu.pyck.cloud/graphql', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${token}`
    },
    body: JSON.stringify({
      query: `
        query {
          me {
            UserID
            Username
            Email
          }
        }
      `
    })
  });

  const result = await response.json();
  return result.data.me;
}
```

## Understanding the response

GraphQL responses always follow the same structure:

```json
{
  "data": {
    "me": {
      "UserID": "0190a1b2-c3d4-7e5f-8a9b-0c1d2e3f4a5b",
      "Username": "john.doe",
      "Email": "john.doe@example.com"
    }
  }
}
```

If there are errors, they'll be returned in an `errors` array:

```json
{
  "errors": [
    {
      "message": "You are not authenticated",
      "locations": [{ "line": 2, "column": 3 }],
      "path": ["me"]
    }
  ],
  "data": null
}
```

## Basic query examples

### Listing inventory items

```graphql
query {
  inventoryItems(first: 10) {
    edges {
      node {
        id
        sku
      }
    }
  }
}
```

### Creating an inventory item

```graphql
mutation {
  createInventoryItem(input: {
    sku: "TP-001"
    data: { weight: 1.5, color: "blue" }
  }) {
    inventoryItem {
      id
      sku
      createdAt
    }
  }
}
```

## Using variables

GraphQL allows you to use variables for dynamic values:

```graphql
query GetItem($id: ID!) {
  nodeInventory(id: $id) {
    ... on InventoryItem {
      id
      sku
    }
  }
}
```

With variables:

```json
{
  "id": "item-123"
}
```

## Next steps

After your first queries, you can:

1. Explore the [API reference](https://docs.pyck.cloud/dev/api.md) to understand available operations
2. Learn about [Authentication](https://docs.pyck.cloud/dev/developers/authentication.md#sending-authenticated-requests) for secure API access
3. Read [Error Handling](https://docs.pyck.cloud/dev/developers/error-handling.md) to handle API errors
4. Read [API conventions](https://docs.pyck.cloud/dev/developers/api-conventions.md) for pagination and request limits
5. Read the documentation for specific queries and mutations

## Tools and resources

- [GraphQL endpoint](#graphql-endpoint): the production and demo endpoints
- [Apollo Client](https://www.apollographql.com/docs/react/): Popular GraphQL client for JavaScript
- [Insomnia](https://insomnia.rest/): API client with GraphQL support
- [Postman](https://learning.postman.com/docs/sending-requests/supported-api-frameworks/graphql/): API testing tool with GraphQL support
