# Items

Source: https://swapcard.dev/content-api/examples/items


Fetch, create, update and delete the items of an event and their categories. An item can be a product, a project, a job offer and so on.

Items are products in the API: the objects are `Product` and `ProductCategory`. Each item belongs to a category, and you can link it to one or more exhibitors.

Reference: [`Product` object](../graphql-event-api-schema/objects/product.mdx), [`ProductCategory` object](../graphql-event-api-schema/objects/product-category.mdx)

On this page you can:

- [Fetch products](#fetch-products), with pagination or filtered by ID or category.
- [Create a product category](#create-product-category) and [create a product](#create-productitem).
- [Update a product category](#product-category-1) and [update a product](#product-1).
- [Delete products](#delete-products).
- Check the [requirements and constraints](#requirements--constraints) to avoid errors.

## Before you begin

- An API key, sent in the `Authorization` header. The key acts with the roles and permissions of the user who created it. See [Authentication](/organizer/authentication).
- The ID of the event (`eventId`). See [Events](./graphql-event-api-examples-event.md).
- To create a product, the ID of its category. Create the category manually or with [`createProductCategoryV2`](#create-product-category).

## Fetch products

Use the `event` query with the `products` field to fetch the products of an event.

### Get the first 100 products
Fetch the first 100 products of an event, together with its product categories.

| Argument | Description |
| --- | --- |
| [`eventId`](../graphql-event-api-schema/queries/event.mdx#id-id) | The ID of the event. |
| `cursor` | Pagination. `first` sets how many records to fetch per page. |

```graphql title="Query"
query EventProducts($eventId: ID!, $cursor: CursorPaginationInput!){
  event(id: $eventId) {
    productCategoriesV2{
      totalCount
      nodes {
        id
        name
      }
    }
    products(cursor: $cursor) {
      nodes {
        clientIds
        id
        name
      }
    }
  }
}
```

```json title="Variables"
{
  "eventId": "RXZlbnRfMQ==",
   "cursor": {
    "first": 100
  }
}
```

### Get the next 100 products
To fetch the next page, send the query again and set `cursor.after` to the `pageInfo.endCursor` value of the previous response. Repeat while `pageInfo.hasNextPage` is `true`.

| Argument | Description |
| --- | --- |
| [`eventId`](../graphql-event-api-schema/queries/event.mdx#id-id) | The ID of the event. |
| `cursor` | Pagination. `first` sets how many records to fetch per page; `after` is the cursor returned by the previous response. |

```graphql title="Query"
query EventProducts($eventId: ID!, $cursor: CursorPaginationInput!) {
  event(id: $eventId) {
    products(cursor: $cursor) {
      nodes {
        clientIds
        id
        name
      }
      pageInfo {
        hasNextPage
        endCursor
      }
    }
  }
}
```

```json title="Variables"
{
  "eventId": "RXZlbnRfMQ==",
  "cursor": {
    "first": 100,
    "after": "INSERT_CURSOR_ID_HERE"  
  }
}
```

### Get products by ID
Fetch specific products.

| Argument | Description |
| --- | --- |
| `eventId` | The ID of the event. |
| `filter.ids` | Array of the IDs of the products to fetch. |

```graphql title="Query"
query EventProducts($eventId: ID!, $filter: ProductFilterInput!) {
  event(id: $eventId) {
    products(filter: $filter) {
      nodes {
        clientIds
        id
        name
      }
    }
  }
}
```

```json title="Variables"
{
  "eventId": "RXZlbnRfNDY1Njc4",
  "filter": {
    "ids":
    [
      "ProductID1",
      "ProductID2"
    ]
  }
}
```

### Get products by category
Fetch the products of one or more categories.

| Argument | Description |
| --- | --- |
| `eventId` | The ID of the event. |
| `filter.parentCategoryIds` | Array of the IDs of the categories. |

```graphql title="Query"
query EventProducts($eventId: ID!, $filter: ProductFilterInput!) {
  event(id: $eventId) {
    products(filter: $filter) {
      nodes {
        clientIds
        id
        name
      }
    }
  }
}
```

```json title="Variables"
{
  "eventId": "RXZlbnRfNDY1Njc4",
  "filter": {
    "parentCategoryIds":
    [
      "ProductCategoryID1",
      "ProductCategoryID2"
    ]
  }
}
```

## Create a product category
Use the `createProductCategoryV2` mutation to create a category, for example "Job Offers".

| Argument | Description |
| --- | --- |
| `eventId` | The ID of the event where you create the category. |
| `name` | The name of the category in the event. |

```graphql title="Mutation"
mutation createProductCategory($input: CreateProductCategoryInput!) {
  createProductCategoryV2(input: $input) {
    errors {
      code
      message
    }
    productCategory {
      id
      name
    }
  }
}
```

```json title="Variables"
{
  "input" : {
    "name":"Job Offers",
    "eventId":"RXZlbnRfMQ==",
    "imageUrl":"www.test.png",
    "color": "blue",
    "limit": 200
  }
}
```

## Create a product
Use the `createProduct` mutation to create a product in a category, and optionally link it to exhibitors.

| Argument | Description |
| --- | --- |
| `eventId` | The ID of the event where you create the product. |
| `categoryId` | The ID of the category of the product. The category must already exist: create it manually or with the [`createProductCategoryV2`](#create-product-category) mutation. |
| `name` | The name of the product in the event. |
| `clientId` | Your own unique external ID for the product. |
| `translations` | The name of the product in other languages. |

```graphql title="Mutation"
mutation createProduct($input: CreateProductInput!) {
  createProduct(input: $input) {
    product {
      clientIds
      customFields {
        ... on TextField {
          id
          locale
          value
        }
      }
      translations {
        name
        language
      }
      description
      name
      updatedAt
      createdAt
      exhibitors {
        name
      }
      assetsUrls
    }
    errors {
      message
    }
  }
}
```

```json title="Variables"
{
  "input" : {
    "clientId": "Item1Id",
    "name": "swapcard item",
    "categoryId":"UHJvZHVjdENhdGVnb3J5XzEwMTYzOQ==" ,
    "eventId":"RXZlbnRfMQ==",
    "imageUrl":"www.test.png",
    "description": "This is a swapcard item",
    "customFields": [{
      "definitionId": "RmllbGREZWZpbml==", 
      "text": {"language": "fr_FR", "value": "item"}
    }],
    "translations": [{
      "name": "swapcard item",
      "language": "en_US"
    }],
    "exhibitorIds": ["ExhibitorId"],
    "assetsUrls": ["www.asset1.com", "www.asse2.com"]
  } 
}
```

## Update a product category
Use the `updateProductCategoryV2` mutation to update a category.

| Argument | Description |
| --- | --- |
| `productCategoryId` | The ID of the category to update. |

```graphql title="Mutation"
mutation updateProductCategory($input: UpdateProductCategoryInput!) {
  updateProductCategoryV2(input: $input) {
    errors {
      code
      message
    }
    productCategory {
      id
      name
    }
  }
}
```

```json title="Variables"
{
  "input" : {
    "name":"Job Offers New",
    "imageUrl":"www.testnew.png",
    "productCategoryId": "UHJvZHVjdENhdGVnb3J5XzEwMTY0MA=="
  }
}
```

## Update a product
Use the `updateProduct` mutation to update a product, for example to change its category, exhibitors or custom fields.

| Argument | Description |
| --- | --- |
| `productId` | The Swapcard ID of the product to update. |
| `categoryId` | The ID of the category of the product. The category must already exist: create it manually or with the [`createProductCategoryV2`](#create-product-category) mutation. |

```graphql title="Mutation"
mutation updateProduct($input: UpdateProductInput!) {
  updateProduct(input: $input) {
    errors {
      code
      message
    }
    product {
      updatedAt
      id
    }
  }
}
```

```json title="Variables"
{
  "input" : {
    "productId": "UHJvZHVjdF82MTQwMjc=",  
    "categoryId":"UHJvZHVjdENhdGVnb3J5XzEwMTYzOQ==" ,
    "exhibitorIds": ["RXhoaWJpdG9yXzY5MDUyNA=="],
    "customFields": [{
      "definitionId": "RmllbGREZWZpbml==", 
      "text": {"language": "fr_FR", "value": "Item updated"}
    }]
  }
}
```

## Delete products

Use the `deleteProducts` mutation to delete one or more products. The response returns the IDs of the deleted products.

| Argument | Description |
| --- | --- |
| `productIds` | Array of the IDs of the products to delete. |

:::warning
Make sure you use the right product IDs and that you have the permission to delete these products.
:::

```graphql title="Mutation"
mutation deleteProducts($input: DeleteProductsInput!){
  deleteProducts(input: $input) {
    deletedProductIds
  }
}
```

```json title="Variables"
{
  "input" : {
    "productIds": [
      "UHJvZHVjdF82MTQwMzY=",
      "UHJvZHVjdG46MTQwMzY="
    ]
  }
}
```

## Requirements and constraints
Check these points to avoid errors when you create or update products:

- Use the right product ID.
- You need the permission to update the product, and all the exhibitors linked to it.
- Use a valid event ID and category ID.
- `name` and `nameTranslation`: 255 characters maximum.
- `description` and `descriptionTranslation`: 2,000 characters maximum.
- A product can have up to 20 linked images.
- Asset URLs must be valid.
- Image URLs must not use `http`.
- To link exhibitors, use valid exhibitor IDs.
