Skip to main content
If you’ve requested an ad and inspected the response in the Ad Serving Quickstart, this page covers what you need to apply ads in your service: building requests with user and page information and custom targeting, handling no-ad and failure cases, and rendering each creative type.

Configure ad requests

Call POST /app/v0/ad/{tagid} from the client (web or app) or a server when an ad is needed. Use the ad unit ID as tagid. If there are no additional conditions, send an empty JSON object {}. For the available fields and response structure, see Serving an ad in the API Reference.

Add request information

After confirming that the minimum request renders an ad, add the information needed for ad selection. Adding fields one at a time and checking the response makes it easier to see which condition stops an ad from being returned.

Custom targeting example

If a line item has custom targeting, its ad is selected only when the request’s targeting matches the conditions. For example, if you register the targeting key gender with the value code male in General > Custom Targeting > Key-Values in Ad Manager and set gender to Include male in the line item’s Custom targeting, send the request as follows.
Use the targeting key and value code, not the display names, and match the case exactly. With multiple conditions, all of them must be met; within one condition, any one of its values is enough. Send values as strings, even numbers, such as "age": "30".

Handle the response

Handle ad responses as one of the following four cases.

No ad

HTTP 204 is a successful response meaning no ad was selected. It has no body, so don’t parse it as JSON. If you expected an ad but received 204, check the settings below. For an example of configuring an AdUnit, creative, and allocation from the beginning, see Ad Serving Quickstart.
These are the basic checks. Whether an ad is finally served also depends on the ad selection logic and other operating conditions. For placement and allocation settings, see Create and Manage Placements, Request an Allocation, and Review Allocations.
Passback is an optional feature that fills the ad area with an external ad tag configured for the ad unit when A2 has no ad. If a tag is configured, the API returns HTTP 200 with ext.passback instead of 204, and the response has no ad or ext.ad_log. Process it only where HTML tags can run, such as the web; in an app, handle it like no ad.
Response (excerpt)

Request failures

Decide how long your service waits for an ad, and end ad loading when that time passes or the request fails. Don’t render a response that arrives afterward. With the default settings, A2 returns HTTP 408 for a request it can’t process within 1 second. When you request again, keep in mind that another impression may be recorded. A2 selects an ad anew for each request and, with On Ad Response, records an impression when it returns an ad. A request the client stopped waiting for may already have recorded an impression. For a 4xx response, check the request path, headers, and body. If errors recur, record the time, ad unit ID, HTTP status, and the x-request-id header if the response includes it. See Verification and Troubleshooting for status-specific checks, including cases where an invalid ad unit ID returns 500.

Render the ad

How you render the ad depends on the creative field in the response. A response can contain more than one of these fields, so check them in the order shown in the table. A native HTML mode response contains ad.display.native along with ad.display.adm, but it holds only impression tracking information, without assets. If you branch on ad.display.native alone, you’ll try to render an HTML mode response with UI components and fail, so check ad.display.adm first. Rendering HTML in an iframe alone does not automatically record an A2 click. Notify the parent document of clicks inside the iframe and send clk with that ad response’s ext.ad_log. See Ad Event Tracking for the event body and delivery instructions.
Map each item in a native response’s assets to a UI component by the native format field identifier in ext.field_name. Replace title, main_image, and cta in the example with the identifiers of the native format used for the creative. This applies to native creatives made in A2. Native ads from external ad networks don’t include ext.field_name.
Response (excerpt)

Next steps

Register the products you advertise in the Product Catalog.