# Generating an asset

This guide creates a board, generates an image onto it and waits for the result. Creating the board takes the workspace id from `GetWorkspace`, and every call takes a key from that workspace; see [Authentication](https://app.plentylabs.com/developers/authentication). Each step shows the published operation document, which you can run as is from the playground or your own code.

## 1. Create a board

Create a board to hold the result, or pick one from `ListBoards`. Keep its `id`: card operations take it as `boardId`.

```graphql
mutation CreateBoard(
  $workspaceId: ID!
  $title: String
  $visibility: BoardVisibility
) {
  createBoard(input: { workspaceId: $workspaceId, title: $title, visibility: $visibility }) {
    success
    board {
      id
      title
      visibility
      createdAt
    }
  }
}
```

## 2. Generate an image

Generation returns at once, with each image in status `QUEUED` and already on the board from step 1 as a card, which shows the image once it finishes. `cardIds` names the cards, in image order. If the cards could not be placed, `cardIds` is null and `errors` holds a `CARDS_NOT_PLACED` error while the images still generate: place them with `PlaceCards`. Keep the image ids. Credits are charged when the mutation is accepted and refunded if the generation fails.

```graphql
mutation GenerateImage(
  $boardId: ID!
  $near: CanvasPlacementNearInput
  $prompt: String!
  $aspectRatio: String!
  $count: Int = 1
  $models: [String!]
  $qualityLevel: ImageQuality
  $resolution: String
  $referenceImages: [ReferenceImageInput!]
  $camera: ImageCameraInput
  $parentImageId: ID
) {
  generateImage(
    input: {
      prompt: $prompt
      aspectRatio: $aspectRatio
      count: $count
      models: $models
      qualityLevel: $qualityLevel
      resolution: $resolution
      referenceImages: $referenceImages
      camera: $camera
      parentImageId: $parentImageId
      boardId: $boardId
      placement: { near: $near }
    }
  ) {
    success
    cardIds
    images {
      id
      status
      aspectRatio
      model
      prompt
    }
    failures {
      model
      count
      error {
        code
        message
      }
    }
  }
}
```

## 3. Get the result

Poll each id until `status` is `COMPLETED` or `FAILED`. Wait 5 seconds before the first poll, then 10, 15 and 20 seconds between polls. A `FAILED` image has an `errorMessage` that says why.

```graphql
query GetAsset(
  $id: ID!
) {
  image: asset(id: $id) {
    ... on Image {
      id
      displayName
      status
      progress
      file {
        url
      }
      originalFile {
        downloadUrl
      }
      errorMessage
      aspectRatio
      prompt
      width
      height
      imageType
      hasReferenceMedia
    }
  }
  video: asset(id: $id) {
    ... on Video {
      id
      displayName
      name
      status
      progress
      file {
        url
        downloadUrl
      }
      thumbnail {
        url
        downloadUrl
      }
      errorMessage
      aspectRatio
      durationMs
      prompt
      model
      draftExpiresAt
      videoType
      hasReferenceMedia
    }
  }
  audio: asset(id: $id) {
    ... on Audio {
      id
      displayName
      name
      source
      status
      progress
      file {
        url
        downloadUrl
      }
      errorMessage
      durationMs
      model
      musicProvider
      createdAt
    }
  }
  artboard: asset(id: $id) {
    ... on Artboard {
      id
      displayName
      aiDisclosure
    }
  }
  job: backgroundJob(id: $id) {
    id
    type
    status
    metadata
    artboardFrames {
      timeSec
      file {
        signedUrl
      }
    }
    artboardExports {
      artboard {
        id
      }
      width
      height
      error
      file {
        downloadUrl
      }
    }
    artboardExportPdf {
      downloadUrl
    }
    createdAt
    completedAt
    memberJobs(first: 200) {
      edges {
        node {
          id
          type
          status
        }
      }
      pageInfo {
        hasNextPage
      }
    }
  }
}
```

An image's `originalFile.downloadUrl`, and a video's or audio's `file.downloadUrl`, is a signed link that works for one hour without signing in. Fetch the asset again for a fresh link. `file.url` loads only inside the PlentyLabs app.

To put an asset that is already in the library, such as an upload, on a board, use `PlaceCards`. Without coordinates, the server places it on clear canvas. An asset from another board is copied, so each board keeps its own.

## Video and audio

`GenerateVideo`, `GenerateMusic`, `GenerateVoiceover` and `GenerateVideoSfx` work the same way: with `boardId`, the result is placed on the board as a card in the same call (check `cardIds` and `errors` as above), it starts `QUEUED`, and you poll it with `GetAsset` until it is `COMPLETED` or `FAILED`. Video takes longer than images, so widen the polling interval.
